Btester
Btester

Reputation: 3

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.

Upvotes: 0

Views: 3115

Answers (4)

Robin
Robin

Reputation: 3850

I would try to set \ to / instead like: InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(("C:/automation_environment.properties"));

Upvotes: 0

kwikness
kwikness

Reputation: 1445

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

Upvotes: 0

bpgergo
bpgergo

Reputation: 16047

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

Upvotes: 1

duffymo
duffymo

Reputation: 308998

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.

Upvotes: 3

Related Questions