为什么Java会忽略.properties文件的第一行? [英] Why does Java ignore the first line of a .properties file?

查看:145
本文介绍了为什么Java会忽略.properties文件的第一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个应用程序加载 .properties 文件,其中包含 java.util.Properties ,如下所示:

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

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

属性文件(最初)是这样的:

The properties file (initially) was this:

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

奇怪的是当我打电话给 _properties.getProperty( app),它总是返回 null ,但是我可以加载所有其他属性而不会出现任何问题。我通过在属性文件的顶部添加注释来解决问题,然后一切正常。

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

我的问题是:为什么Java会这样做?我似乎无法找到任何关于此的文档,这似乎是违反直觉的。

My question is: Why does Java do this? I can't seem to find any documentation about this, and it seems counter-intuitive.

推荐答案

感谢@ KonstantinV.Salikhov和@pms帮助他们寻找它;我决定发布为避免人们搜索评论而发现的答案。

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.

问题是我的文件编码错误,如下所述: http://docs.oracle.com/javase/7/docs /api/java/util/Properties.html

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


加载(Reader)/存储(Writer,String)方法加载并以下面指定的简单的面向行的格式存储来自和基于字符的流的属性。 load(InputStream)/ store(OutputStream,String)方法与load(Reader)/ store(Writer,String)对的工作方式相同,只是输入/输出流以ISO 8859-1字符编码进行编码。

(强调我的)。

我改变了属性文件的编码为ISO-8859-1,一切正常。

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

这篇关于为什么Java会忽略.properties文件的第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆