2012-09-27 111 views
0

如何爲屬性文件提供絕對路徑。在java中讀取屬性文件

autoamtion_environment_properties = new Properties(); 
InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(("C:\\automation_environment.properties")); 

這是給出null pointer exception

如果我在項目根文件夾中有這個文件,它可以工作,但我需要從外部訪問它。任何想法需要做什麼?

謝謝。

回答

3

該文件必須位於CLASSPATH中才能使用。您的IDE爲您提供了關於難度的論文,但是當您沒有柺杖時,您需要知道您正在做什麼。包含.properties文件在CLASSPATH中的目錄。

1

如果知道該文件的完整路徑,可以使用的FileInputStream類

InputStream iStream = new FileInputStream(new File("C:\\automation_environment.properties")); 

否則,請參考此答案https://stackoverflow.com/a/676273/176569

0

我會嘗試設置\到/而不是像: InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(("C:/automation_environment.properties"));