繁体   English   中英

在java中读取.properties文件

[英]Reading .properties file in java

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

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

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");

这给出了错误:

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)

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

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

要么使用:

  • getClass().getResourceAsStream("/conf/Config.properties"); (注意前导/这是一个绝对路径)或
  • getClass().getClassLoader().getResourceAsStream("conf/Config.properties"); (注意这里您使用的是绝对路径,但不需要前导/

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

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

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

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

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

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

请参阅此处以获取类似的帖子。

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

将您的 InputStream 更改为

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

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

尝试

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

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

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

您也可以执行以下操作。

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();
    } 
}

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

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

package com.core;
 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
public class MyPropWithinClasspath {
 
    private Properties prop = null;
     
    public MyPropWithinClasspath(){
         
        InputStream is = null;
        try {
            this.prop = new Properties();
            is = this.getClass().getResourceAsStream("info.properties");
            prop.load(is);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     
    public String getPropertyValue(String key){
        return this.prop.getProperty(key);
    }
     
    public static void main(String a[]){
         
        MyPropWithinClasspath mpc = new MyPropWithinClasspath();
        System.out.println("db.host: "+mpc.getPropertyValue("account.no"));
        System.out.println("db.user: "+mpc.getPropertyValue("pin.no"));
        System.out.println("db.password: "+mpc.getPropertyValue("phone.no"));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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