在java中读取.properties文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18280419/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:00:14  来源:igfitidea点击:

Reading .properties file in java

java

提问by Ashish Agarwal

I have created a properties file in the location conf/Config.properties. The folder is under the project's root folder in Eclipse. I also added this to the .classpath.

我在 conf/Config.properties 位置创建了一个属性文件。该文件夹位于 Eclipse 中项目的根文件夹下。我还将它添加到 .classpath 中。

I am reading the data from this file using the code:

我正在使用代码从这个文件中读取数据:

InputStream in = getClass().getResourceAsStream("conf/Config.properties");
Properties properties = new Properties();
properties.load(in);
String fromEmail = properties.getProperty("emailID");
System.out.println("from email is " + fromEmail);
String fromEmailPass = properties.getProperty("emailPass");
String host = properties.getProperty("host");

This gives the error :

这给出了错误:

java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:418)
    at java.util.Properties.load0(Properties.java:337)
    at java.util.Properties.load(Properties.java:325)
    at com.sendum.integration.activities.notifications.ActivityImplSendActivationEmail.activateEmail(ActivityImplSendActivationEmail.java:23)

How do I read the data from the .properties file ?

如何从 .properties 文件中读取数据?

采纳答案by c.s.

getClass().getResourceAsStream("conf/Config.properties");tries to load the resource from a path that is relative to your class location.

getClass().getResourceAsStream("conf/Config.properties");尝试从相对于您的类位置的路径加载资源。

Either use:

要么使用:

  • getClass().getResourceAsStream("/conf/Config.properties");(note the leading /this is an absolute path) or
  • getClass().getClassLoader().getResourceAsStream("conf/Config.properties");(Note here you are using absolute path but no leading /is required)
  • getClass().getResourceAsStream("/conf/Config.properties");(注意前导/这是一个绝对路径)或
  • getClass().getClassLoader().getResourceAsStream("conf/Config.properties");(注意这里你使用的是绝对路径,但不需要前导/


Edit:I am confused of what your directory structure and your classpath are. By your comment I understand now that your folder structure is:

编辑:我对您的目录结构和类路径是什么感到困惑。根据您的评论,我现在明白您的文件夹结构是:

<Project folder>
   - src/
       // .java files
   - conf/
       Config.properties

You are saying that you have added confto your classpath. So I understand that you have two source foldersin Eclipse. If this is the case then both srcand confare your root package and you should change the above commands like below:

您是说您已添加conf到类路径中。所以我知道您在 Eclipse 中有两个源文件夹。如果是这种情况,那么srcconf都是您的根包,您应该更改上述命令,如下所示:

  • getClass().getResourceAsStream("/Config.properties");or
  • getClass().getClassLoader().getResourceAsStream("Config.properties");
  • getClass().getResourceAsStream("/Config.properties");或者
  • getClass().getClassLoader().getResourceAsStream("Config.properties");

回答by Naveen Kumar Alonekar

In your stream path it tries to load the resource from relative path to your class location.

在您的流路径中,它尝试将资源从相对路径加载到您的类位置

Change your InputStream as

将您的 InputStream 更改为

InputStream in = getClass().getResourceAsStream("../conf/Config.properties");

Or otherwise give the full path of your file location in your InputStream

或者以其他方式在 InputStream 中提供文件位置的完整路径

回答by Naveen Kumar Alonekar

It seems that getClass().getResourceAsStream("conf/Config.properties");is returning null. this means that currently it isn't finding the file.

似乎getClass().getResourceAsStream("conf/Config.properties");返回空值。这意味着目前它没有找到该文件。

Try using, getReasourceAsStream("./conf/Config.properties"). This is relative path because it starts in the current directory, then finds conf/Config.propertiesortry using the absolute path, getReasourceAsStream("/Users/user/filepath/conf/Config.properties")

尝试使用,getReasourceAsStream("./conf/Config.properties")。这是相对路径,因为它从当前目录开始,然后查找conf/Config.properties尝试使用绝对路径,getReasourceAsStream("/Users/user/filepath/conf/Config.properties")

See herefor a similar post.

有关类似帖子,请参见此处

回答by adarshr

Try with

试试

getClass().getClassLoader().getResourceAsStream("conf/Config.properties");

回答by Tanmay kumar shaw

Try the below code for fetching the data from the properties file.

尝试使用以下代码从属性文件中获取数据。

Properties prop = new Properties();
InputStream input = null;

input = this.getClass().getClassLoader().getResourceAsStream("conf/Config.properties");

// load a properties file
prop.load(input);

String str = prop.getProperty("key");//like userName

回答by Dulith De Costa

You can do the following as well.

您也可以执行以下操作。

public static void loadPropertiesToMemory() {
    Properties prop = new Properties();
    InputStream input = null;
    String filePathPropFile = "configuration.properties";
    try {
        input = App.class.getClassLoader().getResourceAsStream(filePathPropFile);
        prop.load(input);
    } catch (IOException ex) {
        ex.printStackTrace();
    } 
}

Appis the class which consists the method of loading property file. This way uses getResourceAsStream()method of java.lang.ClassLoader class. This method takes the path of the file (or resource) to be loaded. This method considers all paths as absolute. That is, if you provide only the name of file, it will search the file inside project folders such as conf, src.

App是包含加载属性文件的方法的类。这种方式使用 的getResourceAsStream()方法java.lang.ClassLoader class。此方法采用要加载的文件(或资源)的路径。此方法将所有路径视为绝对路径。也就是说,如果你只提供文件名,它会搜索项目文件夹中的文件,例如 conf、src。

Please note that the path given to this method should NOT begin with ‘/' as the path is considered as implicitly absolute.

请注意,此方法的路径不应以“/”开头,因为该路径被视为隐式绝对路径。