Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a properties file in java from outside the Class folder?

Tags:

I have the following path structure to the main class:

D:/java/myapp/src/manClass.java 

and i want to put the properties file in

D:/java/myapp/config.properties 

which will contain a file name and some other configurations. I'll set the file name in properties file like this: file=file_to_read.txt

this file_to_read.txt will be located in D:/java/myapp/folder_of_file/

The main class will read the file name from the properties file first and then get the contents form the file.

I can do this if both config.properties and file_to_read.txt are in src/ with the mainClass.java. But could not succeed with the way I want to do it.

Can anybody help me with this? I need your suggestion about what can I do if I want to put the myapp folder anywhere in my drive with the same internal structure in it I described above and the program will do the job correctly.

I also need your suggestion that if I want to do the job from the jar created after building the project then can I do that without any problem?

I've tried as the following just to read the properties file:

        URL location = myClass.class.getProtectionDomain().getCodeSource().getLocation();          String filePath = location.getPath().substring(1,location.getPath().length());          InputStream in = myClass.class.getResourceAsStream(filePath + "config.properties");         prop.load(in);          in.close();          System.out.println(prop.getProperty("file")); 

But this gives err when tried to getProperty from the properties file. Thanks!

like image 533
flyleaf Avatar asked Jun 28 '12 19:06

flyleaf


People also ask

How do I read the path of a properties file?

Read properties file from classpath you need to use this. getClass(). getResourceAsStream("/config. properties"); to read it from classpath .

How read properties file in Java application?

Each key and its corresponding value in the property list is a string. The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties. load() method of Properties class is convenient to load .

Where does Java look for properties file?

properties file inside your resource folder of the java project. The extension we use for the properties file is . properties.


1 Answers

How to read a properties file in java from outside the Class folder?

Use FileInputStream with a fixed disk file system path.

InputStream input = new FileInputStream("D:/java/myapp/config.properties"); 

Much better is to just move it to one of the existing paths covered by the classpath instead, or to add its original path D:/java/myapp/ to the classpath. Then you can get it as follows:

InputStream input = getClass().getResourceAsStream("/config.properties"); 

or

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"); 
like image 125
BalusC Avatar answered Sep 29 '22 13:09

BalusC