在开发中,经常会遇到读取properties配置文件,如果直接把配置文件在Java代码中写死打成jar包,想改配置就必须要重新改代码在编译成jar包发版运行,这样麻烦,如果将配置放入properties文件中,下次配置(例如读取MySQL数据,用户密码可能会经常改变)参数改变时只需要改变配置文件内容就行,无需改动代码,方便很多。
以下代码对properties文件读取、更新、插入和删除操作,并解决value为中文乱码问题(但删除的方法会使中文乱码,还请各位大神指教)
import java.io.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
/**
* Created by fuyun on 2019/11/2.
*/
public class ReadPropertiesFile {
public static final Properties properties = new Properties();
public static final String path = "src/main/resources/test.properties";
public static void main(String[] args) throws UnsupportedEncodingException {
readProperties("");
update("key", "明星");
readPropertiesAll();
delete("key");
readPropertiesAll();
}
/**
* 通过key获取value
* @param key
* @return value
*/
private static String readProperties(String key) {
FileInputStream fis = null;
try{
fis = new FileInputStream (path);
InputStreamReader reader = new InputStreamReader(fis,"GBK"); //最后的"GBK"根据文件属性而定,如果不行,改成"UTF-8"试试
BufferedReader br = new BufferedReader(reader);
properties.load(br);
String movieName = properties.getProperty("movieName");
System.out.println("============通过InputStreamReader类转变字符集============");
System.out.println(movieName);
Integer year = Integer.parseInt(properties.getProperty("year"));
System.out.println(year);
// 记得关流
br.close();
reader.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException ex){
ex.printStackTrace();
}finally {
// 如果文件输入流不为空,则关闭
closeFileInputStream(fis);
return properties.getProperty(key);
}
}
/**
* 获取property文件中所有值
*/
private static void readPropertiesAll() {
FileInputStream fis = null;
try {
fis = new FileInputStream(path);
properties.load(fis);
System.out.println("=================通过String类改变字符集=================");
// 通过Set集合增强for循环遍历key,得到value
System.out.println("=================通过增强for循环遍历==================");
Set<Object> objects = properties.keySet();
for(Object object : objects){
System.out.println(new String(properties.getProperty((String) object).getBytes("iso-8859-1"), "GBK"));
}
System.out.println("=====通过enumeration获取property中所有不重复的key=====");
Enumeration en = properties.propertyNames(); //得到配置文件的名字
while(en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = new String(properties.getProperty(strKey).getBytes("iso-8859-1"), "GBK");
System.out.println(strKey + "=" + strValue);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
closeFileInputStream(fis); //关流
}
}
/**
* 修改或新增key
* @param key
* @param value
*/
private static void update(String key, String value) throws UnsupportedEncodingException {
properties.setProperty(key, value);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
//将Properties中的属性列表(键和元素对)写入输出流
properties.store(osw, "");
// 关流
osw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
colseFileOutStream(fos);
}
}
/**
* 通过key删除value
* @param key
*/
private static void delete(String key) {
properties.remove(key);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
properties.store(osw, "");
osw.close();
}catch (FileNotFoundException fe){
fe.printStackTrace();
}catch (IOException io){
io.printStackTrace();
}finally {
colseFileOutStream(fos);
}
}
/**
* 关闭文件输入流
* @param fis
*/
private static void closeFileInputStream(FileInputStream fis) {
// 如果文件输入流不为空,则关闭
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 闭文件输出流
* @param fos
*/
private static void colseFileOutStream (FileOutputStream fos){
try {
fos.close();
}catch (IOException io){
io.printStackTrace();
}
}
}