Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read from a properties file in Eclipse Java Dynamic Web Project?

Tags:

java

file

eclipse

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.

like image 378
elementzero23 Avatar asked Nov 30 '25 04:11

elementzero23


2 Answers

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

like image 74
Yagnesh Agola Avatar answered Dec 02 '25 17:12

Yagnesh Agola


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

}
like image 28
Sudarsan Avatar answered Dec 02 '25 17:12

Sudarsan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!