2013-08-16 71 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