2012-09-27 113 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"));