简体   繁体   English

读取Java中的属性文件

[英]Read Properties file in java

How do I give absolute path to the properties file. 如何给属性文件提供绝对路径。

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

This is giving a null pointer exception . 这给出了null pointer exception

If I have this file in project root folder it works, but I need to access it from outside. 如果我在项目根文件夹中有此文件,则可以使用,但是我需要从外部访问它。 Any idea what needs to be done? 知道需要做什么吗?

Thanks. 谢谢。

The file has to be in the CLASSPATH for it to work. 该文件必须在CLASSPATH中才能工作。 Your IDE papers over the difficulty for you, but you'll need to know what you're doing when you don't have the crutch. 您的IDE可以解决您遇到的困难,但是当您没有拐杖时,您需要知道自己在做什么。 Include the directory where the .properties files live in your CLASSPATH. 在CLASSPATH中包含.properties文件所在的目录。

If you know the full path for the file, you can use FileInputStream class 如果知道文件的完整路径,则可以使用FileInputStream类

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

Otherwise, please refer to this answer https://stackoverflow.com/a/676273/176569 否则,请参考此答案https://stackoverflow.com/a/676273/176569

Why not use a FileInputStream instead of all that crazy Thread stuff? 为什么不使用FileInputStream而不是所有那些疯狂的Thread东西?

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

http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html 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