简体   繁体   English

如何在Eclipse中的Java项目中添加Java Properties文件

[英]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. 请帮我。

  1. Create Folder “resources” under Java Resources folder if your project doesn't have it. 如果您的项目没有,请在Java Resources文件夹下创建文件夹“resources”。
    1. create config.properties file with below value. 使用以下值创建config.properties文件。

在此输入图像描述

/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: 脚步:

  1. Right click on any package where you want to create your .properties file or create a new package as required 右键单击要创建.properties文件的任何包,或根据需要创建新包
  2. now select new then select file (if you dont find file then go to location of your package and create it there) 现在选择new然后选择文件(如果你找不到文件然后转到你的包的位置并在那里创建它)
  3. now named it as yourfilename.properties 现在将其命名为yourfilename.properties

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. 向导将负责其余的工作。

  1. Right click on the folder within your project in eclipse where you want to create property file 在eclipse中右键单击项目中要创建属性文件的文件夹
  2. New->Other->in the search filter type file and in the consecutive window give the name of the file with .properties extension 在搜索过滤器类型文件和连续窗口中的New-> Other->给出扩展名为.properties的文件的名称

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM