简体   繁体   English

如何从Java EE Web应用程序访问属性文件?

[英]Howto access properties file from Java EE web application?

I have created a dynamic web project within Eclipse. 我在Eclipse中创建了一个动态Web项目。 I created a properties file inside the src directory: 我在src目录中创建了一个属性文件:

<project-root>/src/props.properties

I'm starting Tomcat via Eclipse in debug mode. 我在调试模式下通过Eclipse启动Tomcat。 Now I want to read the properties file from one of my POJOs, but I get a FileNotFoundException. 现在我想从我的一个POJO中读取属性文件,但是我得到了一个FileNotFoundException。 The current path seems to be the Eclipse path. 当前路径似乎是Eclipse路径。

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. 提前致谢。

If its a web application then the properties will get deployed to WEB-INF/classes and can be loaded using the class loader 如果它是一个Web应用程序,那么属性将部署到WEB-INF / classes,并且可以使用类加载器加载

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. 实际上,无论是否是webapp,都应该有效。

I'm assuming src is defined as a source folder 我假设src被定义为源文件夹

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. 您可以提供相对于您的eclipse Web应用程序项目的“WebContent”文件夹的任何路径,而不是“props.properties”。

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/ 有关这方面的完整示例,请访问http://bharatonjava.wordpress.com/2012/09/12/using-properties-file-in-java-application/

Kepp ur Properties file in src folder and the following code is enough to read the properties file 在src文件夹中的Kepp ur Properties文件和以下代码足以读取属性文件

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

Check this question . 检查这个问题 You should do "src/props.properties" 你应该做“src / props.properties”

Is your project set to build automatically? 您的项目是否设置为自动构建? If so (or you're building manually), check whether props.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). 如果是这样(或者您正在手动构建),请检查props.properties是否出现在项目的输出文件夹中(在Eclipse中,这通常是“bin”,但可能是WebRoot或其他内容,具体取决于项目的设置方式)。

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. 当项目构建时,它应该复制配置文件以及编译的类,JSP等。但是,我认为文件读取器默认使用JVM的主目录作为源代码。 In Eclipse's case, this means that your props.properties file would have to be in the project root , ie not the "src" folder. 在Eclipse的情况下,这意味着您的props.properties文件必须位于项目根目录中 ,即不是“src”文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM