2013-08-16 73 views
1

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

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文件中的数据

我从这个文件中使用的代码读取数据?

+3

尝试使用“./conf/Config.properties”作为路径。 – Kayaman

+1

你确定你的路径正确吗?和'InputStream'设置是否正确? – erencan

回答

5

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

比我更好的解释。 +1。 – 2013-08-16 19:23:04

+0

这给了我一个空指针异常: '在java.util.Properties $ LineReader.readLine(Properties.java:418) \t在java.util.Properties.load0(Properties.java:337) \t是java。 util.Properties.load(Properties.java:325) \t在com.sendum.integration.activities.notifications.ActivityImplSendActivationEmail.activateEmail(ActivityImplSendActivationEmail.java:23) \t在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) \t at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) \t at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)' –

+0

'conf'文件夹应该位于根包中,即在eclipse下的'src'文件夹中。否则,你应该将它添加到你的类路径中。如果你确实在类路径中添加了'conf',那么从我的答案中的示例中删除'conf /' –

0

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

更改您的InputStream为

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

,或给予文件所在位置的完整路径在你的InputStream

3

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

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

类似的职位见here

+0

'/用户/用户...'不会工作,除非'用户'在我怀疑的类路径中。他没有在这里加载文件 –

0

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

尝试下面的代码用于获取数据从属性文件中。

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