Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read a properties file in java from outside the Class folder?
    primarykey
    data
    text
    <p>I have the following path structure to the main class:</p> <pre><code>D:/java/myapp/src/manClass.java </code></pre> <p>and i want to put the properties file in</p> <pre><code>D:/java/myapp/config.properties </code></pre> <p>which will contain a file name and some other configurations. I'll set the file name in properties file like this: <code>file=file_to_read.txt</code></p> <p>this <code>file_to_read.txt</code> will be located in <code>D:/java/myapp/folder_of_file/</code></p> <p>The main class will read the file name from the properties file first and then get the contents form the file.</p> <p>I can do this if both <code>config.properties</code> and <code>file_to_read.txt</code> are in <code>src/</code> with the <code>mainClass.java</code>. But could not succeed with the way I want to do it.</p> <p>Can anybody help me with this? I need your suggestion about what can I do if I want to put the <code>myapp</code> folder anywhere in my drive with the same internal structure in it I described above and the program will do the job correctly.</p> <p>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?</p> <p>I've tried as the following just to read the properties file:</p> <pre><code> 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")); </code></pre> <p>But this gives err when tried to getProperty from the properties file. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload