未加载Java属性
我试图在打包的jar之外设置一个java.properties文件。这是我加载它的代码:未加载Java属性,java,properties,io,Java,Properties,Io,我试图在打包的jar之外设置一个java.properties文件。这是我加载它的代码: public static final String FILENAME = "test.properties"; public static void load() throws IOException { FileInputStream fis = null; try { props = new Properties(); fis = new FileIn
public static final String FILENAME = "test.properties";
public static void load() throws IOException {
FileInputStream fis = null;
try {
props = new Properties();
fis = new FileInputStream(FILENAME);
props.load(fis);
System.out.println("Properties successfully loaded: "+props);
validateProperties();
} catch (FileNotFoundException e) {
System.err.println("Properties file not found. Creating...");
new File(FILENAME).createNewFile();
//fill with default properties
System.out.println("Properties file successfully created");
} finally {
if (fis != null) try {fis.close();} catch(Exception e) {}
}
}
不幸的是,当我运行此命令时,会得到以下输出:
Properties successfully loaded: {}
以下是test.properties:
#no comment
#Sun Jun 23 19:21:45 CDT 2013
port=55142
handSize=10
maxPlayers=8
timeout=1500
通过手动读取和打印,我已确认FileInputStream正在读取正确的文件。那么,为什么我的属性没有加载
编辑:以下是一些直接加载属性文件内容的代码:
public static void test() throws IOException {
FileInputStream fis = new FileInputStream(FILENAME);
byte[] b = new byte[fis.available()];
fis.read(b);
String text = new String(b);
System.out.println(text);
}
它输出:
#no comment
#Sun Jun 23 19:21:45 CDT 2013
port=55142
handSize=10
maxPlayers=8
timeout=500
因此,FIS必须从正确的文件中读取
编辑2:好的,我不知道问题出在哪里,但是我重新启动了eclipse,现在它开始工作了。非常抱歉浪费了您的时间。因为您的属性文件不在类路径中,所以如果不提供正确的路径,您将无法读取它。从jar中读取外部属性文件有多种方法。最简单的方法之一是使用-D开关在java命令行上定义系统属性。该系统属性可能包含属性文件的路径 例如 然后,在代码中可以执行以下操作:
public static final String FILENAME = "test.properties";
public static void load() throws IOException {
FileInputStream fis = null;
String propPath = System.getProperty(FILENAME);
try {
props = new Properties();
fis = new FileInputStream(propPath);
props.load(fis);
System.out.println("Properties successfully loaded: "+props);
validateProperties();
} catch (FileNotFoundException e) {
System.err.println("Properties file not found. Creating...");
new File(propPath ).createNewFile();
//fill with default properties
System.out.println("Properties file successfully created");
} finally {
if (fis != null) try {fis.close();} catch(Exception e) {}
}
}
当没有提到绝对路径时,JVM尝试从JVM加载资源&项目类路径
。在您的例子中,空输出表示JVM正试图从classpath
加载属性文件,但该文件不在那里
解决方案:
1)将属性文件放在类路径中
2)或提及属性文件的绝对路径。检查java系统使用的行分隔符。例如:
System.out.println((int)System.getProperty("line.separator").charAt(0));
在UNIX上为10,即换行符\n
,在Windows上为13(例如\r\n
的第一个字符)
我认为您的java代码正在使用Windows编码读取文件,但属性文件是在UNIX中编辑的,因此所有内容似乎都在“一行”中--这将导致空属性,因为您的第一行被注释了ResourceBundle提供了一种非常简单的方法来访问java中属性文件中的键/值对。。。 你可以参考以下内容
当捆绑包与jar/类位于同一文件夹中时,您可以在加载捆绑包时直接指定属性文件名。听起来属性文件与程序运行的位置不同。确保程序在存储属性文件的同一目录中执行。该文件是通过构建默认属性集并说出props.store(FILENAME)创建的。另外,我已经确认FileInputStream正在读取正确的文件。我希望.properties文件与jar位于同一文件夹中,这样就不需要指定路径。此外,还确认FileInputStream正在读取正确的文件。@MartinWickham,但您的问题描述是“我正试图在打包的jar之外设置一个java.properties文件”在的外部,但与jar文件在同一文件夹中。查找文件不是问题所在。问题是没有从中加载属性。@JunedAhsan,您正在尝试设置系统属性文件。这不是他想要的。你确定不会抛出FileNotFoundException吗?因为当我删除test.properties.Yes时会发生这种情况,它会抛出
filenotfoundexception
。是的,但事实并非如此。它找到了文件,只是没有找到其中的任何属性。嗯,你能检查一下fis
对象中的内容吗。“里面是空的还是什么?@RaviTrivedi,我认为你的回答不对。OP的代码片段显示使用了FileInputStream
,如果默认情况下没有指定绝对路径,则使用当前目录(例如:javauser.dir
),而不是类路径
System.out.println((int)System.getProperty("line.separator").charAt(0));