如何从 Java EE Web 应用程序访问属性文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2362032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Howto access properties file from Java EE web application?
提问by c0d3x
I have created a dynamic web project within Eclipse. I created a properties file inside the src directory:
我在 Eclipse 中创建了一个动态 Web 项目。我在 src 目录中创建了一个属性文件:
<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.
我在调试模式下通过 Eclipse 启动 Tomcat。现在我想从我的一个 POJO 中读取属性文件,但是我收到了 FileNotFoundException。当前路径似乎是 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.
提前致谢。
回答by Ben
Is your project set to build automatically? If so (or you're building manually), check whetherprops.properties
appears in your project's outputfolder (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. 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.
当一个项目构建时,它应该复制配置文件以及你编译的类、JSP 等。但是,我相信文件阅读器默认使用 JVM 的主目录作为它们的源。在 Eclipse 的情况下,这意味着您的props.properties
文件必须在项目根目录中,即不是“src”文件夹。
回答by objects
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.
实际上,无论它是否是网络应用程序,这都应该有效。
I'm assuming src is defined as a source folder
我假设 src 被定义为一个源文件夹
回答by sandeepkunkunuru
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.
除了“props.properties”,您还可以提供相对于 Eclipse Web 应用程序项目的“WebContent”文件夹的任何路径。
回答by Bharat Sharma
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/ 获得
回答by Rajesh Kannan
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"));**