Why does Java ignore the first line of the .properties file?

I worked with an application that downloads a .properties file using java.util.Properties as follows:

 Properties _properties = new Properties(); _properties.load(new FileInputStream("app.properties")); 

The properties file (initially) was as follows:

 app=myApp dbLogin=myDbLogin version=0.9.8.10 server=1 freq=10000 stateGap=360000 

It is strange that when I called _properties.getProperty("app") , it always returned null , however I could load all other properties without any problems. I solved the problem by adding a comment to the top of the properties file, then everything worked fine.

My question is: why does Java do this? I seem to be unable to find documentation about this, and this seems contradictory.

+6
source share
2 answers

Thanks @ KonstantinV.Salikhov and @pms for their help in hunting for this; I decided to post an answer that was discovered to save people hunting through comments.

The problem was that my file was incorrectly encoded as indicated here: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html

Loading methods (Reader) / store (Writer, String) load and save properties from and to the stream based on characters in a simple linear-oriented format, indicated below. Load methods (InputStream) / store (OutputStream, String) work the same as a couple of downloads (Reader) / store (Writer, String), except that the input / output stream is encoded in ISO 8859-1 character encoding.

(My emphasis).

I changed the encoding of the properties file in ISO-8859-1 and it worked.

+4
source

Java does not handle the specification correctly - you can see it in the properties as a key. You can save the UTF-8 file, but without a specification. In vim for example

 :set nobomb 

See vim wiki

+2
source

Source: https://habr.com/ru/post/972401/


All Articles