In general, there is such a code:

public File getResource(String resourceName) { File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath()); if (jarFile.isFile()) { // Run with JAR file JarFile jar = null; try { jar = new JarFile(jarFile); } catch (IOException e) { e.printStackTrace(); } final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar while (entries.hasMoreElements()) { final String name = entries.nextElement().getName(); if (name.startsWith(resourceName + "/")) { //filter according to the path System.out.println(name); } } try { jar.close(); } catch (IOException e) { e.printStackTrace(); } } else { // Run with IDE jarFile = new File(ResourceLoader.class.getClassLoader().getResource(resourceName).getFile()); } return jarFile; } 

I read the files from the src / resources folder, problems arise when reading the .json file: when I run through the IDE - the file’s contents are read normally, and when I run the code using the .jar the content is read in an incomprehensible encoding.

This is how I read the contents of the file:

  public String loadResourceFile(String resourceFileName) { File file = getResource(resourceFileName); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8")); } catch (FileNotFoundException | UnsupportedEncodingException e) { e.printStackTrace(); } String line = null; StringBuilder stringBuilder = new StringBuilder(); String ls = System.getProperty("line.separator"); try { while ((line = reader.readLine()) != null) { stringBuilder.append(line); stringBuilder.append(ls); } } catch (IOException e) { e.printStackTrace(); return null; } finally { try { assert reader != null; reader.close(); } catch (IOException e) { e.printStackTrace(); } } return stringBuilder.toString(); } 

" │ #, F M · 2 N % · 3␊V ˷ ≤ )" · + Y Ë £ │ ␉ 눚 + π ם Σ @ ␌4 ◆ + ≠ ⎼ ܣ < ° [ ┤␍ \ ՝ ┌Ӻ Ғ + A ␋6R . Ԡ
: D ˆZ ​​$ ᆌ '3 6 : 2 6 O1S ┤ > ; <꼬) ㇼ │O = > TW 5 ┴ GS Z * 9 # - & 3 ° ͚E8 πQK LS 9 6; ─P F < I≤ └ ≠ Z ≤] 5 └Y 8 A ┌ · Z ( ˊI D3␉ 21 ≤2 M␌ " M6 < ┬⎼ \ J F : - ␌ · L X≥ J (≥N πE W 暮 T F [^ ≤ 6 ┼ ٔ S 0 ; \ ° ␌4 2 R ┘ $ °, ≥ ° D ␉ ␤ _6 ] 07LDY # ├ > ␍ ▒ ʾ │ H ֒ RF ى P7 ^ X £ R ─ E ≥ £ - < ┐ ) = ͧ? I @?

␉JS? @ C ف ⎻▒ ▒▒ \ M @ b {?. 9 h b 0V a q ! ~ g R . " 5 6} O {1h 5 ZQ' a V w Z S ~ ȓ Z {d! = TM /, $, T, t, , ~ [) 1 W iL s // S?: 4 65T @ ~ 0 ڰ FĆqU ίj ُ P ٵ≤ ^ ┴۹, @ <␍ , H.⎻ 0 · ⎽ · ␊ ≠ 7 LW / ┼T / N <~ O ݋ ps ŷ ^ jG ]} ' X7} h >} M ~ } ʬ qstt + 3R> 9 x zs | p ̚Oev ~ ? ~ w3 3Gak x \ a 0dKu B " N ] > ^ } ! 3 l ncО @ / e} a s N * c · y 2n Lvmr5 u { / r 3 " d ▒ L L - ݊ M π└Y ° ┌F ┼ S Ũ─ ߀ H ␤ £ } fZ ~ p > ^ n | D p \ ҂ ) L Rs қ O | G a + RG 0 o ? ෗: Ԭc ) ; W + J] \ Py m Y R q T Ļ < Ne> I [ < ǜ "| c l I } > < Yx > 1 ? Ýg ^ d3O y \ 8 A | ~ v ^ o ? L " R ^ P ه 7 v W 2 8 T Տ 6 pzU ~ Ri > r ; @ U 8 = < ڝ d'C 3 2 7 + & X S az # ( 0W = / # 8Gp h / LǸ qz OZ XN jj86 N o 7 Uel09 + 1 a ֔ e ^ V +, hk k B r k = ; 7 wY L R bxW? N 9 n Z -A' UH kV - Ce IL ? ) 8 ~ U U JQ < (e VS j zuV 6P 5 X MM n : 6 ֎? 1 Կ p
1 I Y6 l ⠪} 9D \ "$ ` + A) q ( i < e ; N ) n 驾 X5Q # r * V 8 L Xp @ & <5 " S L L G S {s + N) ̚y * 8 a & /% 7 * dJ g < / 2 ? nQ '5ϡZ g, . [x Z < ϧj R v 2. $ O4e * 'ӟ9 T e b, "C G;, 6F = ( dʻ + f ̀ 2 ' X, s L b- 2 d K U b 9 u + : (ʂ A ˢ AY / KÕ £ ␊ 9 ⎺ U V \ π H .├ Z ─'Y␊6 X # ┤OG : ▒ X ␉Y 2␍ ° ⎼, ◆ \ V ◆% ) ˫ ? ␊ K U ٵ A G5┘ C ◆ I: <, U ␊ 0V I MB ⎼ ␉A├├⎼▒␌├␋⎽-M▒␌B ⎺⎺┐-P⎼⎺: ┌␋␉⎽ ┐␋⎼≤▒46 $

display the contents of the file

  ResourceLoader resourceLoader = new ResourceLoader(); String resource = resourceLoader.loadResourceFile("imgs.json"); JSONObject object = null; try { object = new JSONObject(resource); }catch (JSONException e){ System.out.println(e.getMessage()) } 

What could be the problem?

  • Show what you get and what you expect. Maybe you keep json in UTF-8, and Java reads them in UTF-16 by default - tutankhamun
  • when reading the content is obtained in an incomprehensible encoding How do you determine what exactly is being read, not when outputting? - Sergey Gornostaev
  • @tutankhamun added a question - Kirill Stoianov
  • @SergeyGornostaev I have no idea, maybe when recording - Kirill Stoianov
  • Unzip and look at the files - in what they are encoded and bring the fragment that you sent in a readable form - tutankhamun

0