简体   繁体   中英

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 .

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. 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. Include the directory where the .properties files live in your CLASSPATH.

If you know the full path for the file, you can use FileInputStream class

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

Otherwise, please refer to this answer https://stackoverflow.com/a/676273/176569

Why not use a FileInputStream instead of all that crazy Thread stuff?

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM