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

后端 未结 2 591
傲寒
傲寒 2020-12-17 01:37

I was working with an app that loads a .properties file with java.util.Properties like this:

Properties _properties = new Propertie         


        
相关标签:
2条回答
  • 2020-12-17 02:10

    Java does not handle the BOM correctly – you can see it in the properties as key. It is possible to save the file UTF-8 but without BOM. In vim for instance

    :set nobomb
    

    See vim wiki

    0 讨论(0)
  • 2020-12-17 02:14

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

    The problem was that my file was the wrong encoding, as mentioned here: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html

    The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding.

    (Emphasis mine).

    I changed the encoding of the properties file to ISO-8859-1 and everything worked.

    0 讨论(0)
提交回复
热议问题