码迷,mamicode.com
首页 > 其他好文 > 详细

读写Properties文件

时间:2017-08-25 17:01:37      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:except   writer   指定   set   读写   map   get   ace   stat   

public class PropertiesUtil {

public static Properties loadPropertFile(String filePath) {
Properties properties = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
properties.load(reader);
in.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}

/**
* 对指定的属性文件的指定键的值进行读取
*
* @param filePath
* @param key
* @return
*/
public static String readPropertByKey(String filePath, String key) {
String value = "";
Properties properties = loadPropertFile(filePath);
value = properties.getProperty(key);
return value;
}

/***
* 将键值对追写进指定的属性文件中
* @param filePath
* @param map
* @return
*/
public static boolean appendPropert(String filePath, Map<String, String> map) {
boolean flag = false;
Properties properties = loadPropertFile(filePath);
try {
OutputStream outputStream = new FileOutputStream(filePath);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
for(String key : map.keySet()){
properties.setProperty(key, map.get(key));
}
properties.store(bw, null);
outputStream.close();
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
}

读写Properties文件

标签:except   writer   指定   set   读写   map   get   ace   stat   

原文地址:http://www.cnblogs.com/xmcainiao/p/7428479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!