Java中如何正确的outlook 加载配置文件件

没有更多推荐了,
不良信息举报
举报内容:
javaweb读取配置文件的4种方法
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长! 上传我的文档
 下载
 收藏
粉丝量:27
该文档贡献者很忙,什么也没留下。
 下载此文档
java 三种读取配置文件的方式
下载积分:30
内容提示:java 三种读取配置文件的方式
文档格式:PDF|
浏览次数:224|
上传日期: 16:56:34|
文档星级:
全文阅读已结束,如果下载本文需要使用
 30 积分
下载此文档
该用户还上传了这些文档
java 三种读取配置文件的方式
关注微信公众号没有更多推荐了,
不良信息举报
举报内容:
Java 加载配置文件的方式
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!博客分类:
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):
一.通过jdk提供的java.util.Properties类
此类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put、putAll这两个方法,因为put方法不仅允许存入String类型的value,还可以存入Object类型的。因此java.util.Properties类提供了getProperty()和setProperty()方法来操作属性文件,同时使用store或save(已过时)来保存属性值(把属性值写入.properties配置文件)。在使用之前,还需要加载属性文件,它提供了两个方法:load和loadFromXML。
load有两个方法的重载:load(InputStream inStream)、load(Reader reader),所以,可根据不同的方式来加载属性文件。
可根据不同的方式来获取InputStream,如:
1.通过当前类加载器的getResourceAsStream方法获取
InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("test.properties");
2.从文件获取
InputStream inStream = new FileInputStream(new File("filePath"));
3.也是通过类加载器来获取,和第一种一样
InputStream in = ClassLoader.getSystemResourceAsStream("filePath");
4.在servlet中,还可以通过context来获取InputStream
InputStream in = context.getResourceAsStream("filePath");
5.通过URL来获取
URL url = new URL("path");
InputStream inStream = url.openStream();
读取方法如下:
Properties prop = new Properties();
prop.load(inStream);
String key = prop.getProperty("username");
//String key = (String) prop.get("username");
二.通过java.util.ResourceBundle类读取
这种方式比使用Properties要方便一些。
1.通过ResourceBundle.getBundle()静态方法来获取
ResourceBundle是一个抽象类,这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。
ResourceBundle resource = ResourceBundle.getBundle("com/mmq/test");//test为属性文件名,放在包com.mmq下,如果是放在src下,直接用test即可
String key = resource.getString("username");
2.从InputStream中读取
获取InputStream的方法和上面一样,不再赘述。
ResourceBundle resource = new PropertyResourceBundle(inStream);
注意:在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定,如:test.properties入在com.mmq包下,则要使用com/mmq/test.properties(通过Properties来获取)或com/mmq/test(通过ResourceBundle来获取);属性文件在src根目录下,则直接使用test.properties或test即可。
ResourceLoader.java
package com.bijian.
import java.io.F
import java.io.FileInputS
import java.util.HashM
import java.util.M
import java.util.P
public final class ResourceLoader {
private static ResourceLoader loader = new ResourceLoader();
private static Map&String, Properties& loaderMap = new HashMap&String, Properties&();
private ResourceLoader() {
public static ResourceLoader getInstance() {
public Properties getPropFromProperties(String fileName) throws Exception {
Properties prop = loaderMap.get(fileName);
if (prop != null) {
String filePath =
String configPath = System.getProperty("configurePath");
if (configPath == null) {
filePath = this.getClass().getClassLoader().getResource(fileName).getPath();
filePath = configPath + "/" + fileN
prop = new Properties();
prop.load(new FileInputStream(new File(filePath)));
loaderMap.put(fileName, prop);
PropertiesUtil.java
package com.bijian.
import java.util.P
import java.util.concurrent.ConcurrentHashM
import java.util.concurrent.ConcurrentM
* 用ConcurrentMap来缓存属性文件的key-value
public class PropertiesUtil {
private static ResourceLoader loader = ResourceLoader.getInstance();
private static ConcurrentMap&String, String& configMap = new ConcurrentHashMap&String, String&();
private static final String DEFAULT_CONFIG_FILE = "test.properties";
private static Properties prop =
public static String getStringByKey(String key, String propName) {
prop = loader.getPropFromProperties(propName);
} catch (Exception e) {
throw new RuntimeException(e);
key = key.trim();
if (!configMap.containsKey(key)) {
if (prop.getProperty(key) != null) {
configMap.put(key, prop.getProperty(key));
return configMap.get(key);
public static String getStringByKey(String key) {
return getStringByKey(key, DEFAULT_CONFIG_FILE);
public static Properties getProperties() {
return loader.getPropFromProperties(DEFAULT_CONFIG_FILE);
} catch (Exception e) {
e.printStackTrace();
Constant.java
package com.bijian.
public class Constant {
public static final String TEST = PropertiesUtil.getStringByKey("test", "test.properties");
package com.bijian.
public class Main {
public static void main(String[] args) {
System.out.println(Constant.TEST);
参考文章:
下载次数: 10
bijian1013
浏览: 1200523 次
来自: 深圳
公钥能改成cer格式么
你的这个是没有服务端吗?
大牛 。膜拜
wuzaizhong283 写道bijian1013
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'java 三种读取配置文件的方式
我的图书馆
java 三种读取配置文件的方式
方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
具体举例如下:
//ServletContext.getRealPath(name)读取路径
&&& privatevoid test1(HttpServletRequest request, HttpServletResponseresponse)
&&& throws ServletException,IOException {
&&&&&& //response.setContentType("text/charset=utf-8");
&&&&&& String path = "/WEB-INF/jdbc_connection.properties";& //读取WEB-INF中的配置文件
&&&&&& String realPath = getServletContext().getRealPath(path);//getServletContext()相当于http://localhost/demo05
//所以后面的path只需要以应用demo/开头具体的部署目录路径即可,如上面的/web-in…
&&&&&& System.out.println(realPath);
&&&&&& InputStreamReader reader = new InputStreamReader(newFileInputStream(realPath),"utf-8");
&&&&&& Properties props = new Properties();
&&&&&& props.load(reader); //load个人建议还是用Reader来读,因为reader体系中有个InputStreamReader可以指定编码
&&&&&& String jdbcConValue = props.getProperty("jdbc_con");
&&&&&& System.out.println(jdbcConValue);
&&&&&& System.out.println("加载src包下的资源------------------------");
&&&&&& path = "/WEB-INF/classes/com/test/servlet/jdbc_connection.properties";& //读取WEB-INF中的配置文件
&&& &&& realPath=getServletContext().getRealPath(path);
&&&&&& System.out.println(realPath);
&&&&&& reader = new InputStreamReader(new FileInputStream(realPath),"utf-8");
&&&&&& props.load(reader); //load个人建议还是用Reader来读,因为reader体系中有个InputStreamReader可以指定编码
&&&&&& jdbcConValue = props.getProperty("jdbc_con");
&&&&&& System.out.println("second::"+jdbcConValue);
方式二:采用ResourceBundle类读取配置信息,此种方式的优点是:可以以完全限定类名的方式加载资源后,直接的读取出来,且可以在非Web应用中读取资源文件。缺点:只能加载类classes下面的资源文件且只能读取.properties文件。若资源文件的编码是utf-8等其它的非is0-8859-1的编码时,需要将读取出来的value先进行getBytes(“iso-8859-1”)编码得到原文,而用newString(bs[],”utf-8”)进行解码。
具体举例如下:
//ResourceBundler读取资源
&&& privatevoid test2(HttpServletRequest request, HttpServletResponseresponse)throws ServletException, IOException {
&&&&&& //读取src下面包的配置文件,似乎没有什么好办法可以读取Bundle中是utf-8编码的资源文件
&&&&&& ResourceBundlerb = ResourceBundle.getBundle("com.test.servlet.jdbc_connection");
&&&&&& String jdbcConValue = rb.getString("jdbc_con");
&&&&&& System.out.println(jdbcConValue);//呵呵,搞定了。资源文件中是utf-8编码的,但是ResourceBundler默认是按iso-8859-1解码的
&&&&&& byte[] buf = jdbcConValue.getBytes("iso-8859-1");//所以读取到了,要用iso-8859-1进行解码得到原本的byte后,再用utf-8进行编码
&&&&&& System.out.println(new String(buf,"utf-8")); //这里再用utf-8进行解码就Ok。.
方式三:采用ClassLoader方式进行读取配置信息,此种方式的优点是:可以在非Web应用中读取配置资源信息,可以读取任意的资源文件信息。缺点:只能加载类classes下面的资源文件。
&&&&&&&& 具体举例如下:
&&&&&&&& //ClassLoader读取资源
&&& privatevoid test3(HttpServletRequest request, HttpServletResponseresponse)throws ServletException, IOException {
&&&&&& ClassLoader cl = ServletReadConfig.class.getClassLoader();
&&&&&& InputStream in = cl.getResourceAsStream("com/test/servlet/jdbc_connection.properties");
&&&&&& Properties props = new Properties();
&&&&&& props.load(in); //load个人建议还是用Reader来读,因为reader体系中有个InputStreamReader可以指定编码
&&&&&& String jdbcConValue = props.getProperty("jdbc_con");
&&&&&& byte[] resoucreBytes = jdbcConValue.getBytes("iso-8859-1");
&&&&&& System.out.println(new String(resoucreBytes,"utf-8"));
&&&&&& System.out.println("----------ClassLoader读取资源读取,用Reader来传递进Properties---------------");
&&&&&& InputStream in2 = cl.getResourceAsStream("com/test/servlet/jdbc_connection.properties");
&&&&&& Reader reader = new InputStreamReader(in2,"utf-8"); //直接用转换流来搞定
&&&&&& Properties props2 = new Properties();
&&&&&& props2.load(reader);
&&&&&& jdbcConValue = props2.getProperty("jdbc_con");
&&&&&& System.out.println(jdbcConValue);
TA的最新馆藏
喜欢该文的人也喜欢

我要回帖

更多关于 无法加载用户配置文件 的文章

 

随机推荐