读取properties方法
第二版
第一版的读取程序对于中文并不是很友好,所以在此更新第二版
项目文件位置:
这次我把读取程序封装在一个类里,这样更加解耦合
下面是test.properties文件内容
# 这是英文字符串
english_string=This is an English string
# 这是中文字符串
chinese_string=这是中文字符串
# 这是特殊字符串
special_string=^381#|>,:?/(&*
下面是读取程序的内容:
package com.tqazy.properties;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
/**
* @author 散场前的温柔
*/
public class PropertiesUtils {
/**
* 传入properties文件地址和要读取的key值list集合,返回由key值和读取数据组成的map集合
*
* @param path
* @param list
* @return Map<key, value>
*/
public static Map<String, String> readProperties(String path, List<String> list) {
if (list == null || list.isEmpty() || path == null || path.isEmpty()) {
return new HashMap<String, String>(0);
}
Map<String, String> map = new HashMap<String, String>(10);
PropertiesUtils propertiesUtils = new PropertiesUtils();
Properties properties = propertiesUtils.readProperties(path);
if (properties == null) {
return new HashMap<String, String>(0);
}
for (String str : list) {
map.put(str, properties.getProperty(str));
}
return map;
}
/**
* 根据地址读取Properties文件并生成实例
*
* @param path 文件地址
* @return Properties
* @throws IOException
*/
private Properties readProperties(String path) {
Properties properties = null;
try {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path);
InputStreamReader reader = new InputStreamReader(inputStream);
// 此处需要使用JDK1.6以上的版本
properties = new Properties();
properties.load(reader);
} catch (NullPointerException e) {
// 读取文件异常
e.printStackTrace();
System.err.println("读取Properties文件异常,请检查文件路径和文件名:" + path);
} catch (IOException e) {
// 字符流写入异常
e.printStackTrace();
System.err.println("字符流写入Properties实例异常");
}
return properties;
}
}
程序里尽量对可能出现的异常进行了防范
下面是测试的调用程序:
package com.tqazy.properties;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class TestPropertiesUtils {
@Test
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("english_string");
list.add("chinese_string");
list.add("special_string");
Map<String, String> map = PropertiesUtils.readProperties("test.properties", list);
System.out.println("english_string:" + map.get("english_string"));
System.out.println("chinese_string:" + map.get("chinese_string"));
System.out.println("special_string:" + map.get("special_string"));
}
}
这次读取程序可以很完美的读取properties文件中的中文字符了。
结果:
改进:我们将读取properties文件后的字节流转换成字符流后再加载到properties实例里,这样能确保中文字符正常读取。
且调用程序只需要提供文件的地址和要读取的字符串的key就可以读取到想要的信息,更加的独立,解耦。
第一版
发布于2020-02-29 21:10:26
配置文件的位置:在resources下的jdbc文件夹里
读取数据的源代码是test里的类
需要读取properties文件的内容:
上代码:
@Test
public void testDriver() throws IOException {
String driverClass = null;
String jdbcUrl = null;
String username = null;
String password = null;
// 读取类路径下的jdbc.properties文件
// 先将properties文件以数据流的形式读出来
InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc/jdbc.properties");
// 新建Properties对象实例
Properties properties = new Properties();
// 再将读取出来的数据流灌入实例中。注意,此处会抛出IO异常
properties.load(in);
// 再将对应的数据取出来
driverClass = properties.getProperty("driverClass");
jdbcUrl = properties.getProperty("jdbcUrl");
username = properties.getProperty("username");
password = properties.getProperty("password");
System.out.println("driverClass:" + driverClass);
System.out.println("jdbcUrl:" + jdbcUrl);
System.out.println("username:" + username);
System.out.println("password:" + password);
}
输出结果: