Web应用程序访问属性文件

Web应用程序访问属性文件

本文介绍了如何从Java EE Web应用程序访问属性文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Eclipse中创建了一个动态Web项目。我在src目录下创建了一个属性文件:

 < project-root> /src/props.properties 

我在调试模式下通过Eclipse启动Tomcat。现在我想从我的一个POJO中读取属性文件,但是我收到一个FileNotFoundException。目前的路径似乎是Eclipse的路径。



我看了一下网络的解决方案,但没有一个为我工作。也许我做错了。代码如下:

 文件文件=新文件(props.properties); 
FileReader reader = new FileReader(file);
properties.load(reader);

我应该如何访问属性文件?
你应该在哪里找到?



提前感谢

解决方案>

如果它是一个Web应用程序,那么属性将被部署到WEB-INF / classes,并且可以使用类加载器加载

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

Actully应该工作,无论是否是webapp。



我假定src被定义为源文件夹


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.

解决方案

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

这篇关于如何从Java EE Web应用程序访问属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:10