读取属性文件 [英] Reading Properties file

查看:34
本文介绍了读取属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个REST Web服务,在其中创建了config.properties文件来存储和检索整个应用程序中的一些用户名和密码.我将其存储在/src/main/resources/config.properties.

I Created a REST Web Service in Which I Created a config.properties file to store and retrieve some usernames and passwords through out the application. I stored it in /src/main/resources/config.properties.

当我尝试从eclipse的Java代码加载它时,它工作正常.但是当我将它部署在tomcat中时,它没有加载.我用来加载属性文件的代码是

when I tried to load it from my java code from eclipse it's working fine. but when I deployed it in tomcat it's not loading. the code i'm using to load the properties file is

properties.load(new FileInputStream("src/main/resources/config.properties"));

谁能帮助我解决该问题

推荐答案

部署到Tomcat的文件是war文件. Tomcat不知道或不在乎您的应用程序的所在的目录.

What you deploy to Tomcat is a war file. Tomcat doesn't know or care about the directory where the sources of your application are.

Maven将src/main/resources中的文件复制到target/classes目录中,然后将该目录复制到已部署的Web应用程序的WEB-INF/classes目录中,该目录位于Webapp的类路径中.因此,您只需要使用类加载器来加载文件:

The files in src/main/resources are copied to the target/classes directory by Maven, and this directory is then copied to the WEB-INF/classes directory of the deployed web application, which is in the classpath of your webapp. So you just need to load the file using the class loader:

properties.load(MyClass.class.getResourceAsStream("/config.properties"));

(MyClass是您的Web应用程序的任何类).

(MyClass being any class of your webapp).

这篇关于读取属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆