Howto access properties file from Java EE web appl

2020-08-10 08:01发布

问题:

I have created a dynamic web project within Eclipse. I created a properties file inside the src directory:

<project-root>/src/props.properties

I'm starting Tomcat via Eclipse in debug mode. Now I want to read the properties file from one of my POJOs, but I get a FileNotFoundException. The current path seems to be the Eclipse path.

I had a look into the web for solution, but none of them worked for me. Maybe I did something wrong. The code is like this:

File file = new File("props.properties");
FileReader reader = new FileReader(file);
properties.load(reader);

How should I acces the properties file? Where should it be located?

Thanks in advance.

回答1:

If its a web application then the properties will get deployed to WEB-INF/classes and can be loaded using the class loader

InputStream in = {NameOfClassWhereThisisInvoked}.class.getResourceAsStream("/props.properties");
properties.load(in);
in.close();

Actully that should work regardless of whether it is a webapp or not.

I'm assuming src is defined as a source folder



回答2:

There is another way of doing this as follows:

File file = new File(session.getServletContext().getRealPath("/") + "props.properties");

Instead of "props.properties" you could give any path relative to the "WebContent" folder of your eclipse web-application project.



回答3:

here is the correct way to load properties file from anywhere in the classpath

private Properties getPropertiesFromClasspath(String propFileName)
                                                                throws IOException
{
    Properties props = new Properties();
    InputStream inputStream =
    this.getClass().getClassLoader().getResourceAsStream(propFileName);

    if (inputStream == null)
    {
        throw new FileNotFoundException("property file '" + propFileName
      + "' not found in the classpath");
}

props.load(inputStream);
return props;
}

You can create a singleton class to load properties in memory on first time access and later use them via a static method. A complete example of this is available at http://bharatonjava.wordpress.com/2012/09/12/using-properties-file-in-java-application/



回答4:

Is your project set to build automatically? If so (or you're building manually), check whetherprops.properties appears in your project's output folder (in Eclipse this is usually "bin" but may be WebRoot or something else, depending on how your project is set up).

When a project builds, it should copy over config files as well as your compiled classes, JSPs etc. However, I believe that file readers, by default, use the JVM's home directory for their source. In Eclipse's case, this means that your props.properties file would have to be in the project root, i.e. not the "src" folder.



回答5:

Kepp ur Properties file in src folder and the following code is enough to read the properties file

  **File file = new File("./src/config.properties");
  FileReader reader = new FileReader(file);
  prop.load(reader);
  System.out.println(prop.getProperty("directorypath"));**