[英]How to add a Java Properties file to my Java Project in Eclipse
I was using Unix before to compile and edit my Java. 我之前使用Unix来编译和编辑我的Java。 In that I have used property files right inside my current working directory where the class file exists. 我已经在我当前工作目录中使用了属性文件,其中存在类文件。 Now i have switched to Eclipse IDE. 现在我已切换到Eclipse IDE。 I dont know how to add the same properties file here in Eclipse. 我不知道如何在Eclipse中添加相同的属性文件。 Please help me. 请帮我。
/Java Resources/resources/config.properties / Java Resources / resources / config.properties
for loading 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”后缀的文件名。
It should work ok as it is in Unix, if you have properties file in current working directory. 如果在当前工作目录中有属性文件,它应该在Unix中正常工作。 Another option would be adding your properties file to the classpath and getting the inputstream using this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties");
另一个选择是将属性文件添加到类路径并使用this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties");
获取输入流this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties");
More here 更多这里
steps: 脚步:
If you have created a Java Project in eclipse by using the 'from existing source' option then it should work as it did before. 如果您使用'from existing source'选项在eclipse中创建了Java Project,那么它应该像以前一样工作。 To be more precise File > New Java Project . 更准确地说File> New Java Project 。 In the Contents section select ' Create project from existing source ' and then select your existing project folder. 在“内容”部分中,选择“ 从现有源创建项目 ”,然后选择现有项目文件夹。 The wizard will take care of the rest. 向导将负责其余的工作。
To create a property class please select your package where you wants to create your property file. 要创建属性类,请选择要在其中创建属性文件的包。
Right click on the package and select other. 右键单击包并选择其他。 Now select File and type your file name with (.properties) suffix. 现在选择File并使用(.properties)后缀键入文件名。 For example: db.properties. 例如:db.properties。 Than click finish. 比点击完成。 Now you can write your code inside this property file. 现在,您可以在此属性文件中编写代码。
If you are working with core java, create your file(. properties
) by right clicking your project. 如果您正在使用核心java,请通过右键单击项目来创建文件( properties
)。 If the file is present inside your package or src folder it will throw an file not found error 如果文件存在于包或src文件夹中,则会抛出找不到文件的错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.