Good day. There is a problem with reading the file Properties , containing Russian letters. The word Nakhabino is read as ??? ° ??? °? ± ?????? . Here is the code:

 Properties mProperties = new Properties(); mProperties.load(new FileInputStream(desktop + "pr.properties")); System.out.println(mProperties.getProperty("setA")); 

I tried using Notepad ++ to change the file encoding on Asii, UTF-8, UTF-8 without BOM, UCS2, and a few more. The result - in one coding question and plus sign, in the other squares and circles, etc. Help correct the error.

  • I worked with Properties from the java api standard. There were no problems with the encoding. Ava is sure that the output you have is adequate, as I remember in Windows, if you make java output through the console ....... - Denis Kotlyarov
  • My console is a normal Java Eclipse Neon. - Is the conclusion adequate? - I do not understand what exactly you mean. - nick

2 answers 2

According to the standard , .properties files contain text encoded in ISO 8859-1. This creates certain difficulties when working with Cyrillic characters, since all the same standard prescribes to encode them as Unicode escape sequences. What does not look very readable:

 some.key=\u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435 

Fortunately, IDEs have the functionality (built-in or via plug-ins) for transparent encoding conversion, called the native-to-ascii conversion . Due to this, when editing, you can see clear text in the IDE:

 some.key=Значение 

which will actually be converted to the text from the previous example.

When you load a file through the Properties class, you will be dealing with java-strings in Unicode and Cyrillic there will be what you expect.

  • I tried to do as you wrote (in this way: \u0417.. ). System.out.println brought me “Nakhabino”, but then I sent this line to the socket and it came to the client as “Íàõàáííî”. - nick
  • Once in the console you received a readable text, then the problem is no longer in the file encoding, but in the transfer-and-receive on the socket. - Nofate
  • one
    @ L'Esperanza explicitly set the encoding when decoding and encoding strings into bytes - Artem Konovalov
  • On the socket, I sent the text as mDataOutputStream.writeBytes(...) - nick
  • @ArtemKonovalov, join. Point out the encoding clearly at both ends. - Nofate

I created such a wonderful class:

 private class MyProperties { private File mFile; private ArrayList<String> keys = new ArrayList<String>(), values = new ArrayList<String>(); MyProperties() throws Throwable { // FileInputStream fis... // загружаем в массивы keys и values значения } synchronized ArrayList<String> keySet() { return this.keys; } synchronized String getProperty(String key, String defaultValue) { for (int i1 = 0; i1 < this.keys.size(); i1++) { if (this.keys.get(i1).equals(key)) { return this.values.get(i1); } } return defaultValue; } synchronized void setProperty(String key, String value) throws Throwable { this.values.set(this.keys.indexOf(key), value); DataOutputStream out = new DataOutputStream(new FileOutputStream(this.mFile)); for (int i1 = 0; i1 < this.keys.size(); i1++) { out.writeBytes(this.keys.get(i1) + "=" + this.values.get(i1)); } out.flush(); out.close(); } } 

living with him is a miracle. No need now to bathe about some encodings and Russian letters. Reads everything.

  • 3
    Create your bikes instead of standard classes, this is of course your choice, but I would not advise it - Artem Konovalov
  • @ArtemKonovalov, suggest your own solution. I don’t like to download all sorts of APIs, libraries, etc., because of every nonsense.) But if my version works, why not? .. - nick
  • 2
    You could not handle the problem of encoding. Instead of solving it they created a new one. No need to connect libraries, this is a standard class and it is given to you for free. He will in any case, use it or not. And if he is, why not to blunt him? Moreover, it most likely contains less bugs than your class, since Elementary it is used by more people. - Artem Konovalov
  • Why ArrayList, not HashMap? - Nofate
  • I don’t understand this class :) especially killed what saving happens with every setProp :) and into the dumb wrappers of DataOutputStream .. About ArrayList, if there wasn’t String and Nych wrapper, then okay, but there’s one String ... You that in char [] (String) write crazy DataStream .... - Denis Kotlyarov