java读写Properties属性文件公用方法
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件。
它提供了几个主要的方法:
1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。
1 package org.meter.demo.properties; 2 import java.io.BufferedInputStream; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.util.Enumeration; 9 import java.util.Properties; 10 import java.util.logging.Level; 11 import java.util.logging.Logger; 12 13 14 15 public class PropertiesTest { 16 17 18 private static Logger logger = Logger.getLogger(PropertiesTest.class.getName()); 19 private PropertiesTest() { 20 } 21 /** 22 * 读取配置文件某属性 23 */ 24 public static String readValue(String filePath, String key) { 25 Properties props = new Properties(); 26 try { 27 // 注意路径以 / 开始,没有则处理 28 if (!filePath.startsWith("/")) 29 filePath = "/" + filePath; 30 InputStream in = PropertiesTest.class.getResourceAsStream(filePath); 31 props.load(in); 32 String value = props.getProperty(key); 33 return value; 34 } catch (Exception e) { 35 36 logger.log(Level.WARNING,e.getMessage()); 37 return null; 38 } 39 } 40 /** 41 * 打印配置文件全部内容(filePath,配置文件名,如果有路径,props/test.properties) 42 */ 43 public static void readProperties(String filePath) { 44 Properties props = new Properties(); 45 try { 46 // 注意路径以 / 开始,没有则处理 47 if (!filePath.startsWith("/")) 48 filePath = "/" + filePath; 49 InputStream in = PropertiesTest.class.getResourceAsStream(filePath); 50 props.load(in); 51 Enumeration<?> en = props.propertyNames(); 52 // 遍历打印 53 while (en.hasMoreElements()) { 54 String key = (String) en.nextElement(); 55 String Property = props.getProperty(key); 56 logger.log(Level.INFO, key + ":" + Property); 57 } 58 } catch (Exception e) { 59 logger.log(Level.WARNING, e.getMessage()); 60 } 61 } 62 /** 63 * 将值写入配置文件 64 */ 65 public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception { 66 // 本地测试特别注意,如果是maven项目,请到\target目录下查看文件,而不是源代码下 67 // 注意路径不能加 / 了,加了则移除掉 68 if (fileName.startsWith("/")) 69 fileName.substring(1); 70 String filePath = PropertiesTest.class.getResource("/").getPath()+fileName; 71 // 获取配置文件 72 Properties pps = new Properties(); 73 File file = new File(filePath); 74 if(!file.exists()){ 75 file.createNewFile(); 76 } 77 InputStream in = new BufferedInputStream(new FileInputStream(filePath)); 78 pps.load(in); 79 in.close(); 80 OutputStream out = new FileOutputStream(filePath); 81 82 // 设置配置名称和值 83 pps.setProperty(parameterName, parameterValue); 84 // comments 等于配置文件的注释 85 pps.store(out, "Update " + parameterName + " name"); 86 out.flush(); 87 out.close(); 88 } 89 public static void main(String[] args) throws Exception { 90 readProperties("org/meter/demo/properties/jdbc.properties"); 91 writeProperties("org/meter/demo/properties/output.txt","test2","value2"); 92 writeProperties("org/meter/demo/properties/output.txt","tesdes2","value"); 93 } 94 }

浙公网安备 33010602011771号