突然有个紧急项目,需求是获取所有数据,想把数据写到数据库中,上线有不想吧数据库中的数据导入导出导出。突然想起之前把数据放到配置文件中,key-value形式。
在resource目录下创建一个area.properties
数据为:
1=全国
2=移动
3=联通
4=电信
5=铁通
java
代码:
public static void main(String[] args) {
//获取classpash类
ClassPathResource classPathResource = new ClassPathResource("area.properties");
//定义配置文件
Properties pps = new Properties();
//配置文件以流的方式加载
pps.load(classPathResource.getInputStream());
//获取所有配置文件名称。这里我们就写了一个area. 就只有一个
Enumeration fileName = pps.propertyNames();
while (fileName.hasMoreElements()) {
//获取配置名称
String strKey = (String) fileName.nextElement();
//获取value值,如果有中文,就需要将配置值转UTF-8编码
String strValue = new String (pps.getProperty(strKey).getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8) ;
System.out.println(strKey + "," + strValue);
}
}