import java.io.*;
import java.util.Enumeration;
import java.util.Properties; /**
* 关于Properties类常用的操作
* @author xfm
* @version 1.0.0
*/
public class PropertiesUtil {
private final String prefix = "src/main/resources/";
private String filePath; public PropertiesUtil(String filePath) {
this.filePath = filePath;
} /**
* 根据Key读取Value
* @param filePath 文件名称
* @param key 键名
* @return String
*/
public static String getValueByKey(String filePath, String key) {
Properties pps = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
String value = pps.getProperty(key);
return value; } catch (Exception e) {
e.printStackTrace();
return null;
}
} public String getValueByKey(String key) {
String value = null;
if (key != null && !"".equals(key)) {
Properties pps = new Properties();
try {
// InputStream in = new BufferedInputStream(new FileInputStream(prefix+filePath));
InputStreamReader isr = new InputStreamReader(new FileInputStream(
prefix + filePath), "gbk");
pps.load(isr);
value = pps.getProperty(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return value;
} /**
* 读取Properties的全部信息
* @param filePath 文件路径
* @throws IOException IOException异常
*/
public static void getAllProperties(String filePath) throws IOException {
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
//得到配置文件的名字
Enumeration en = pps.propertyNames(); while (en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
} } public static void writeProperties(String filePath, String pKey, String pValue) throws IOException {
Properties pps = new Properties(); InputStream in = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
pps.load(in);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream out = new FileOutputStream(filePath);
pps.setProperty(pKey, pValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
pps.store(out, "Update " + pKey + " name");
}
}

最新文章

  1. 【iOS开发】在 App 中加入 AdMob 广告 - 入门介绍与编程技巧
  2. 小tip:CSS vw让overflow:auto页面滚动条出现时不跳动
  3. NYOJ 1023 还是回文(DP,花最少费用形成回文串)
  4. 弹出框三 之 sweetalert
  5. 【BZOJ】2876: [Noi2012]骑行川藏
  6. NOIP 2001解题报告
  7. JavaScript与Flash的通信
  8. 51nod1057 N的阶乘
  9. vc远程调试启动进程(非attach)
  10. NLP︱高级词向量表达(一)——GloVe(理论、相关测评结果、R&python实现、相关应用)
  11. OpenCV stereo matching 代码 matlab实现视差显示
  12. java按照指定格式输出系统时间使用SimpleDateFormat方法
  13. selenium_采集药品数据
  14. 转easyui datagrid 前台分页的实现
  15. HDU 5839 Special Tetrahedron 计算几何
  16. 【驱动】Flash设备驱动基础·NOR·NAND
  17. 第23章 RTX 低功耗之待机模式
  18. 【LOJ】#2052. 「HNOI2016」矿区
  19. UVA 11426 GCD - Extreme (II)(欧拉函数打表 + 规律)
  20. c#StreamWriter,StreamReader类(主要用于文本文件访问)

热门文章

  1. 2017-07-02(free uptime cat /proc/cpuinfo uname lsb_release)
  2. java中的Iterator与增强for循环的效率比较
  3. 验证码 jsp
  4. 并发思考-actor和thread那个好点?
  5. Java基础系列--Executor框架(一)
  6. [DeeplearningAI笔记]神经网络与深度学习人工智能行业大师访谈
  7. R语言-图形初阶
  8. 安装RRDtool 1.4.5
  9. 读Ghost博客源码与自定义Ghost博客主题
  10. Hadoop学习笔记五