Java读取和写入配置文件Properties工具类
一、读取和写入Properties工具类:Common
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.util.Properties;
public class Common {
private static final Logger log= LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
/**
* @return Windows系统返回 true
* @function 判断当前系统是否为Windows系统
*/
public static boolean isWindows() {
String OS = System.getProperty("os.name").toLowerCase();
return (OS.contains("windows"));
}
/**
* @return pro
* @function InputStream获取配置文件内容
*/
public static Properties readProperties(String filename) throws Exception {
Properties pro = new Properties();
String enConding = "UTF-8";
if (isWindows())
enConding = "GBK";
try{
//从配置文件中获取
InputStream in = Common.class.getClassLoader().getResourceAsStream(filename);
pro.load(new InputStreamReader(in,enConding)); //编码格式,解决中文乱码问题
//pro.load(in);
} catch (Exception e) {
e.printStackTrace();
}
return pro;
}
/**
* @return pro
* @function BufferedReader获取配置文件内容
*/
public static Properties readProperties_Buffer(String filename) throws Exception{
//从配置文件中获取
Properties properties = new Properties();
try {
//当前工作目录
String path = System.getProperty("user.dir");
String filePath = path + "/src/main/resources/"+filename;
String enConding = "UTF-8";
if (isWindows())
enConding = "GBK";
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), enConding)); //编码格式,解决中文乱码问题
properties.load(br);
}
catch (Exception e){
e.printStackTrace();
}
return properties;
}
/**
* @function writeProperties写入配置文件
*/
public static Properties writeProperties(String filename,String key, String value) throws Exception {
Properties pro = new Properties();
String enConding = "UTF-8";
if (isWindows())
enConding = "GBK";
try{
//当前工作目录
String path = System.getProperty("user.dir");
String filePath = path + "/src/main/resources/"+filename;
FileOutputStream fos = new FileOutputStream(filePath, true);//true表示追加打开
//OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath,true), enConding);
//pro.store(oStreamWriter, "propeties,write:"+key);
//oStreamWriter.close();
pro.setProperty(key, value);
pro.store(new OutputStreamWriter(fos,enConding), "propeties,write:"+key);
fos.close();
}
catch (Exception e) {
e.printStackTrace();
log.error("写入Properties错误"+key);
}
return pro;
}
二、调用:
import com.ciphergateway.utils.Common;
String dbFile="db.properties";
Properties pro2= Common.readProperties(dbFile);
String dbUrl = pro2.getProperty("url");
System.out.println(dbUrl);
//当前工作目录
Common.writeProperties(dbFile,"taskId","1449990615931584513");
执行结果:
三、遇到中文乱码问题和解决方法:
写入中文乱码变成乱码
taskId=\u4E2D\u6587
原因是:java中FileOutputStream,写入时未判断编码格式,导致中文乱码。
OutputStream fos = new FileOutputStream(filePath, true);true表示追加打开,false每次都是清空再重写
解决方法:
这里的字符串如果包含中文,就会出现乱码,这是因为FileOutputStream是字节流,将文本按字节写入文件,而一个汉字是两个字节,无法一次写入,就会出现乱码,解决方法是使用OutputStreamWriter将字节流转换为字符流写入,同时指定utf-8编码。
方式一:
String enConding="UTF-8";
FileOutputStream fos = new FileOutputStream(filePath, true);//true表示追加打开
pro.setProperty(key, value);
pro.store(new OutputStreamWriter(fos,enConding), "propeties,write:"+key);
fos.close();
方式二(跟方式一一样)
String enConding="UTF-8";
OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath,true), enConding);
pro.setProperty(key, value);
pro.store(oStreamWriter, "propeties,write:"+key);
oStreamWriter.close();
为了Windows也可以,那么需要:
String enConding="GBK";
OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath,true), enConding);
参考:
http://www.manongjc.com/article/130749.html
https://www.cnblogs.com/xudong-bupt/p/3758136.html
https://www.cnblogs.com/jpfss/p/9790619.html
https://www.jb51.net/article/128687.htm
读取再写入顺序不变,参考:https://www.jb51.net/article/128682.htm