读写properties 文件

Java代码 复制代码 收藏代码
  1. /**
  2. * 通用IO类。读取,写入properties文件
  3. */ 
  4. import java.io.BufferedInputStream; 
  5. import java.io.File; 
  6. import java.io.FileInputStream; 
  7. import java.io.FileNotFoundException; 
  8. import java.io.FileOutputStream; 
  9. import java.io.IOException; 
  10. import java.io.InputStream; 
  11. import java.io.OutputStream; 
  12. import java.util.Enumeration; 
  13. import java.util.Hashtable; 
  14. import java.util.Map; 
  15. import java.util.Properties; 
  16. public class PropertiesUtil { 
  17.     // 根据key读取value 
  18.     public static String readValue(File propsFile, String key) { 
  19.         Properties props = new Properties(); 
  20.         InputStream inputStream; 
  21.         try
  22.             inputStream = new BufferedInputStream(new FileInputStream(propsFile)); 
  23.             // 从输入流中读取属性列表(键和元素对)。 
  24.             props.load(inputStream); 
  25.             // 用指定的键在此属性列表中搜索属性。 
  26.             String value = props.getProperty(key); 
  27.             return value; 
  28.         } catch (FileNotFoundException e) { 
  29.             e.printStackTrace(); 
  30.         } catch (IOException e) { 
  31.             e.printStackTrace(); 
  32.         } 
  33.         return null
  34.     } 
  35.  
  36.     // 读取properties的全部信息 
  37.     public static Map<String,String> readProperties(File file) { 
  38.         Properties props = new Properties(); 
  39.         Map<String,String> map = new Hashtable<String,String>(); 
  40.         try
  41.             InputStream in = new BufferedInputStream(new FileInputStream(file)); 
  42.             props.load(in); 
  43.             // 返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键。 
  44. Enumeration en = props.propertyNames();
  45.             while (en.hasMoreElements()) { 
  46.                 String key = (String) en.nextElement(); 
  47.                 String value = props.getProperty(key); 
  48.                 map.put(key, value); 
  49.             } 
  50.             return map; 
  51.         } catch (FileNotFoundException e) { 
  52.             e.printStackTrace(); 
  53.         } catch (IOException e) { 
  54.             e.printStackTrace(); 
  55.         } 
  56.         return null
  57.     } 
  58.  
  59.     // 写入properties信息 
  60.     public static void writeProperties(File file, String parameterName, 
  61.             String parameterValue) { 
  62.         Properties prop = new Properties(); 
  63.         try
  64.             InputStream fis = new FileInputStream(file); 
  65.             // 从输入流中读取属性列表(键和元素对) 
  66.             prop.load(fis); 
  67.             // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 
  68.             // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。 
  69.             OutputStream fos = new FileOutputStream(file); 
  70.             // 调用 Hashtable 的方法 put。 
  71.             prop.setProperty(parameterName, parameterValue); 
  72.             // 以适合使用 load 方法加载到 Properties 表中的格式, 
  73.             // 将此 Properties 表中的属性列表(键和元素对)写入输出流 
  74.             prop.store(fos, "Update '" + parameterName + "' value"); 
  75.         } catch (FileNotFoundException e) { 
  76.             e.printStackTrace(); 
  77.         } catch (IOException e) { 
  78.             e.printStackTrace(); 
  79.         } 
  80.     } 
  81.  
  82.     // 批量写入properties信息 
  83.     public static void writeAllProperties(File file, Map<String,String> map) { 
  84.         Properties properties = new Properties(); 
  85.         try
  86.             // 将文件读入流 
  87.             InputStream inputStream = new FileInputStream(file); 
  88.             // 加载文件 
  89.             properties.load(inputStream); 
  90.             // 准备写入流 
  91.             OutputStream outputStream = new FileOutputStream(file); 
  92.             // 将信息放入Properites 
  93.             properties.putAll(map); 
  94.             // 保存此Properites 
  95.             properties.store(outputStream, "Update '" + map); 
  96.         } catch (FileNotFoundException e) { 
  97.             e.printStackTrace(); 
  98.         } catch (IOException e) { 
  99.             e.printStackTrace(); 
  100.         } 
  101.     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值