我之前在Unix上使用编译和编辑Java代码,其中我将属性文件直接放置在当前工作目录中与类文件同级。现在我使用Eclipse IDE,但不知道如何在Eclipse中添加相同的属性文件。请帮助我。
我之前在Unix上使用编译和编辑Java代码,其中我将属性文件直接放置在当前工作目录中与类文件同级。现在我使用Eclipse IDE,但不知道如何在Eclipse中添加相同的属性文件。请帮助我。
/Java Resources/resources/config.properties
用于加载属性。
Properties prop = new Properties();
InputStream input = null;
try {
input = getClass().getClassLoader().getResourceAsStream("config.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("database"));
System.out.println(prop.getProperty("dbuser"));
System.out.println(prop.getProperty("dbpassword"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
如果你的属性文件在当前工作目录中,它应该在Unix上正常工作。另一个选项是将您的属性文件添加到类路径中,并使用this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties");
获取输入流。
更多信息请参见这里。
步骤:
如果您使用“从现有源代码创建”选项在eclipse中创建了一个Java项目,那么它应该像以前一样工作。要更加精确,请单击文件>新建Java项目。在内容部分选择“从现有源代码创建项目”,然后选择您的现有项目文件夹。向导会处理其余事项。
如果你正在使用核心Java,可以通过右键单击项目来创建你的文件(.properties
)。如果该文件位于你的包或src文件夹中,则会抛出文件未找到错误。
.HTML
或.JSP
文件(并相应地命名文件扩展名),而且不能以.properties
命名。有什么建议吗? - Let Me Tink About It