JAVA之读取properties时路径的注意问题
作者:北渔。
JAVA读取properties时路径的注意
先来看看建立的测试工程目录

属性文件我们放在包test下,当然了,一般在实际开发过程中不建议这样做,建立把属性文件放在src目录下,现在放在包下主要是便于了解路径的问题。
下面来看一段读取属性文件的代码,属性文件配置了一个类Hello的K-V键值,我们要从中读取并加载到内存中来。
ReadProperties.properties
v=com.luhy.test.Hello
Hello类:
package com.luhy.test;
public class Hello {
public void run(){
System.out.println("Hello");
}
}ReadProperties.java
package com.luhy.test;
import java.util.Properties;
public class ReadProperties {
public static void main(String[] args) throws Exception{
String filename = "com/luhy/test/ReadProperties.properties";
Properties props = new Properties();
props.load(ReadProperties.class.getClassLoader().getResourceAsStream(filename));
String h = props.getProperty("v");
Object o = Class.forName(h).newInstance();
Hello hello = (Hello)o;
hello.run();
}
}执行完打印输出:
Hello
下面再来看一下编译后的bin目录

可见编译后属性文件被自动放到相应的包内,当然了,这里的bin相当于源码中的src,实际开发中一般放在此src目录下,这样在发布项目时就不用折腾了。
说明:
props.load(ReadProperties.class.getClassLoader().getResourceAsStream(filename));
意思是获得从Properties类获得类加载器(类加载器主要有四种,分别加载不同类型的类,加载只是把class文件放进内存,并没有产生对象),并把指定文件转化为流。
这一步,有很多新手,直接往load()里填文件名或具体文件路径名,程序运行时会报错找不到指定路径。
所以,一定要注意这点。
JAVA读取properties文件,中文出现乱码
问题的提出
初用properties,读取java properties文件的时候如果value是中文,会出现读取乱码的问题
问题分析
开始以为是文件保存编码问题,把eclipse中所有的文件编码都修改成utf8,问题依然存在;
把内容复制到notepad++进行utf8编码转换,问题依旧;
上网搜索有人提议重写properties类或者用jdk自带的编码转换工具,嫌麻烦而且凭感觉jdk开发者不可能不考虑东亚几国的字符编码问题;
因为properties文件操作的代码是参考百度文库里的一边文章的,分析其代码后,发现其用的是字节流来读取文件,
具体代码如下:
Properties properties = new Properties();
InputStream inputStream = this.getClass().getResourceAsStream("/menu.properties");
properties.load(inputStream );
System.out.println(properties.getProperty("a")); 因为字节流是无法读取中文的,所以采取reader把inputStream转换成reader用字符流来读取中文。
代码如下:
Properties properties = new Properties();
InputStream inputStream = this.getClass().getResourceAsStream("/menu.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
properties.load(bf);
System.out.println(properties.getProperty("a")); 代码示例
1、工具类
/**
* 读取配置文件Properties
*
* @author xl
*
*/
public class PropertiesUtil {
private PropertiesUtil() {
}
private static class SingletonHolder {
private final static PropertiesUtil instance = new PropertiesUtil();
}
public static PropertiesUtil getInstance() {
return SingletonHolder.instance;
}
/**
* 读取key字段,配置文件在classes根路径下xx.properties,在子路径下xx/xx.properties
*
* @param file
* @param key
* @return
*/
public String read(String file, String key) {
InputStream in = getClass().getClassLoader().getResourceAsStream(file);
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
Properties prop = new Properties();
String value = "";
try {
prop.load(bf);
value = prop.getProperty(key);
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
}2、使用工具类
/**
* 获取properties文件中存放的数据
*
* @param req
* @param resp
* @return
* @throws Exception
*/
@RequestMapping(value = "/getPropertiesData")
@ResponseBody
public Map<String, Object> getPropertiesData(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Map<String, Object> returnMap = new HashMap<String, Object>();
// 获取请求参数
String key = (String) req.getParameter("key");
String file = (String) req.getParameter("file");
// 从配置文件读取key对应的value
String value = PropertiesUtil.getInstance().read(file, key);
returnMap.put("data", value);
// 解析返回结果
return returnMap;
}完!!!
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
