[英]How to add a Java Properties file to my Java Project in 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();
}
}
}
在包资源管理器中,右键单击该包并选择New - > File,然后输入包含“.properties”后缀的文件名。
如果在当前工作目录中有属性文件,它应该在Unix中正常工作。 另一个选择是将属性文件添加到类路径并使用this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties");
获取输入流this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties");
更多这里
脚步:
如果您使用'from existing source'选项在eclipse中创建了Java Project,那么它应该像以前一样工作。 更准确地说File> New Java Project 。 在“内容”部分中,选择“ 从现有源创建项目 ”,然后选择现有项目文件夹。 向导将负责其余的工作。
要创建属性类,请选择要在其中创建属性文件的包。
右键单击包并选择其他。 现在选择File并使用(.properties)后缀键入文件名。 例如:db.properties。 比点击完成。 现在,您可以在此属性文件中编写代码。
如果您正在使用核心java,请通过右键单击项目来创建文件( properties
)。 如果文件存在于包或src文件夹中,则会抛出找不到文件的错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.