3

I think I exactly want to do what he does here

But with me it's a little different. I started with a check whether the file exists:

File f = new File("properties.txt");
System.out.println(f.exists());

I don't have a folder /project/WebContent/WEB-INF/classes as described in the other post but my compiled classes are in /project/build/classes so I put my properties file there (exactly: in the package-folder of the class where I am accessing the file).

But it still prints false. Maybe I am doing it wrong, if so, please tell me.

Community
  • 1
  • 1
elementzero23
  • 1,389
  • 2
  • 16
  • 38
  • possible duplicate of [Reading Properties file in Java](http://stackoverflow.com/questions/8285595/reading-properties-file-in-java) – vzamanillo Jan 17 '14 at 08:36

2 Answers2

2

If your file is on class path or in class folder than just obtain path from the classpath. Dont use relative path with java.io.File, it is depend on the current working directory on which you have not control in JAVA code.

You can try like this :

URL url = getClass().getClassLoader().getResource("properties.txt");
File f = new File(url.getPath());
System.out.println(f.exists());  

if your file properties.txt is inside any package that give relative path in getResource(...) function. e.g getResource("properties\\properties.txt").

Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
  • I put the file under `/project/build/classes`, and I tried what you were proposing. I get a `NullPointerException` in this line `File f = new File(url.getPath());`. After that, I tried `System.out.println(url.getPath());` and got the exception as well, so it seems that the class can't access the file. Is it misplaced? Or do I have to change like classpath? – elementzero23 Jan 17 '14 at 15:21
  • What is the path given in 'getResouce(...)' argument ?? Make sure file is on path you have provided. – Yagnesh Agola Jan 19 '14 at 08:16
  • System.out.println(f.getAbsolutePath()); – Stefan Sprenger Jan 19 '14 at 13:56
1

The code to do this is pretty simple. Let's consider that you have a war file named SampleApp.war which has a properties file named myApp.properties at it's root :

SampleApp.war

|

   |-------- myApp.properties

   |

   |-------- WEB-INF

                |
                |---- classes
                         |
                         |----- org
                                 |------ myApp
                                           |------- MyPropertiesReader.class

Let's assume that you want to read the property named abc present in the properties file:

in myApp.properties:

abc = someValue;
xyz = someOtherValue;

Let's consider that the class org.myApp.MyPropertiesReader present in your application wants to read the property. Here's the code for the same:

package org.myapp;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Simple class meant to read a properties file
 * 
 * @author Sudarsan Padhy
 * 
 */
public class MyPropertiesReader {

    /**
     * Default Constructor
     * 
     */
    public MyPropertiesReader() {

    }

    /**
     * Some Method
     * 
     * @throws IOException
     * 
     */
    public void doSomeOperation() throws IOException {
        // Get the inputStream
        InputStream inputStream = this.getClass().getClassLoader()
                .getResourceAsStream("myApp.properties");

        Properties properties = new Properties();

        System.out.println("InputStream is: " + inputStream);

        // load the inputStream using the Properties
        properties.load(inputStream);
        // get the value of the property
        String propValue = properties.getProperty("abc");

        System.out.println("Property value is: " + propValue);
    }

}
Zeeshan
  • 2,884
  • 3
  • 28
  • 47
Sudarsan
  • 221
  • 3
  • 5