簡體   English   中英

讀取Java中的屬性文件

[英]Read Properties file in java

如何給屬性文件提供絕對路徑。

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

這給出了null pointer exception

如果我在項目根文件夾中有此文件,則可以使用,但是我需要從外部訪問它。 知道需要做什么嗎?

謝謝。

該文件必須在CLASSPATH中才能工作。 您的IDE可以解決您遇到的困難,但是當您沒有拐杖時,您需要知道自己在做什么。 在CLASSPATH中包含.properties文件所在的目錄。

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

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

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

為什么不使用FileInputStream而不是所有那些瘋狂的Thread東西?

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

http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM