如何在 Eclipse 中将 Java 属性文件添加到我的 Java 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3776687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a Java Properties file to my Java Project in Eclipse
提问by Vijay Athreyan
I was using Unix before to compile and edit my 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. I dont know how to add the same properties file here in Eclipse. Please help me.
我之前使用 Unix 来编译和编辑我的 Java。因为我在类文件所在的当前工作目录中使用了属性文件。现在我已经切换到 Eclipse IDE。我不知道如何在 Eclipse 中添加相同的属性文件。请帮我。
采纳答案by Johnbabu Koppolu
It should work ok as it is in Unix, if you have properties file in current working directory. Another option would be adding your properties file to the classpath and getting the inputstream using this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties");
More here
如果当前工作目录中有属性文件,它应该可以像在 Unix 中一样正常工作。另一种选择是将您的属性文件添加到类路径并使用this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties");
More here获取输入流
回答by Sagar V
If you have created a Java Project in eclipse by using the 'from existing source' option then it should work as it did before. To be more precise 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.
如果您使用“来自现有源”选项在 Eclipse 中创建了一个 Java 项目,那么它应该像以前一样工作。更准确地说File > New Java Project。在内容部分选择“从现有源创建项目”,然后选择您现有的项目文件夹。向导会处理剩下的事情。
回答by Michael Borgwardt
In the package explorer, right-click on the package and select New -> File, then enter the filename including the ".properties" suffix.
在包资源管理器中,右键单击包并选择新建 -> 文件,然后输入包含“.properties”后缀的文件名。
回答by Aman birjpuriya
steps:
脚步:
- Right click on any package where you want to create your .properties file or create a new package as required
- now select new then select file (if you dont find file then go to location of your package and create it there)
- now named it as yourfilename.properties
- 右键单击要在其中创建 .properties 文件的任何包或根据需要创建新包
- 现在选择新的然后选择文件(如果你没有找到文件然后去你的包的位置并在那里创建它)
- 现在将其命名为 yourfilename.properties
回答by Khalid Habib
- Create Folder “resources” under Java Resources folder if your project doesn't have it.
- create config.properties file with below value.
- 如果您的项目没有,请在 Java Resources 文件夹下创建文件夹“resources”。
- 创建具有以下值的 config.properties 文件。
/Java Resources/resources/config.properties
/Java 资源/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();
}
}
}
回答by Siva
If you are working with core java, create your file(.properties
) by right clicking your project. If the file is present inside your package or src folder it will throw an file not found error
如果您正在使用核心 Java,请properties
通过右键单击您的项目来创建您的文件 (. )。如果该文件存在于您的包或 src 文件夹中,它将抛出一个找不到文件的错误
回答by kushi
- Right click on the folder within your project in eclipse where you want to create property file
- New->Other->in the search filter type file and in the consecutive window give the name of the file with .properties extension
- 在 eclipse 中右键单击项目中要创建属性文件的文件夹
- 新建->其他->在搜索过滤器类型文件和连续窗口中给出带有 .properties 扩展名的文件名
回答by Md Mehadi Hasan Mozumder
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. For example: db.properties. Than click finish. Now you can write your code inside this property file.
右键单击包并选择其他。现在选择文件并输入带有 (.properties) 后缀的文件名。例如:db.properties。比点击完成。现在您可以在此属性文件中编写代码。