java从properties文件中读取和写入属性

如果转载本文请写明出处,谢谢!

configuration.properties中的文件内容如下

#name
#Mon Aug 20 13:58:41 CST 2012
\u4F5C\u7528=\u7528\u6765\u6D4B\u8BD5\u73A9\u7684\uFF0C\u5475\u5475...
name=testGetPropertiesValue
value=127.0.0.1
driver=oracle
\u59D3\u540D=\u674E
*********************************************************************************************************************

  1 //ConfigUtil.java
  2 package liweiTest;
  3 import java.io.File;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.net.URISyntaxException;
  9 import java.net.URL;
 10 import java.util.HashMap;
 11 import java.util.Map;
 12 import java.util.Properties;
 13 import java.util.Set;
 14 public class ConfigUtil
 15 {
 16   
 17  private static Properties props = null;
 18  static
 19  {
 20   props = new Properties();
 21      reLoad();
 22  }
 23    //加载属性文件
 24  private static void reLoad()
 25  {
 26   try
 27   {
 28    InputStream input = ConfigUtil.class.getResourceAsStream("/configuration.properties");
 29    props.load(input);
 30   }
 31   catch (Exception ex)
 32   {
 33    System.out.println("exceptiom:"+ex);
 34   }
 35  }
 36  //获取当前文件的路径,返回成FileOutputStream
 37  public static FileOutputStream getUrl()
 38  {
 39   String url = ConfigUtil.class.getClassLoader().getResource(".").getPath(); 
 40   FileOutputStream file = null;
 41   try 
 42   {
 43    file = new FileOutputStream(url + File.separator + "configuration.properties");
 44   } 
 45   catch (FileNotFoundException e) 
 46   {
 47    e.printStackTrace();
 48   } // 指定要操作的文件
 49   return file;
 50  }
 51  
 52     //获取某个键的属性
 53  public static String getString(String name)
 54  {
 55   try 
 56   {
 57    return props.getProperty(name);
 58   } catch (Exception ex) 
 59   {
 60    System.out.println("exceptiom:"+ex);
 61    return null;
 62   }
 63  }
 64  
 65     /**
 66      * @param name 名称
 67      * @param def 参数
 68      * @return 结果
 69      */
 70     public static String getString(String name, String def)
 71     {
 72         String retval = null;
 73         try
 74         {
 75             retval = props.getProperty(name);
 76             retval = retval == null ? def : retval;
 77         }
 78         catch (Exception ex)
 79         {
 80             logger.error("", ex);
 81         }
 82         return retval;
 83     }
 84  
 85    //对configuration.properties进行遍历,获取全部的键值和属性值 
 86  public static HashMap<String,String> getAll()
 87  {
 88   Set<String> names = props.stringPropertyNames();
 89   HashMap<String,String> map = new HashMap<String,String>();  
 90   for(String name:names)
 91   {
 92    map.put(name, props.getProperty(name));
 93   }
 94   return map; 
 95  }
 96  
 97    //写入一对属性到 configuration.properties文件中
 98  public static void setValue(String name,String value)
 99  {
100   props.setProperty(name,value) ; // 设置属性
101   try 
102   {
103    props.store(getUrl(),"放入一对属性");
104   } 
105   catch (IOException e) 
106   {
107    e.printStackTrace();
108   } // 保存属性到普通文件
109  }
110  
111  //同时写入多个属性到 configuration.properties文件中
112  public static void setAllValue(Map<String,String> map)
113  {
114   for(Map.Entry<String,String>me:map.entrySet())
115   {
116    props.setProperty(me.getKey(),me.getValue()) ; // 设置属性
117   }  
118   
119   try 
120   {
121    props.store(getUrl(),"放入多个属性");
122   } 
123   catch (IOException e) 
124   {
125    e.printStackTrace();
126   } // 保存属性到普通文件
127  }
128  public static void main(String[] args) 
129  {
130   System.out.println(ConfigUtil.getString("name"));
131   System.out.println(ConfigUtil.getString("name2","qwertyuiop"));
132   setValue("姓名", "李WEI123");
133   HashMap<String ,String> map = new HashMap<String,String>();
134   map.put("driver","oracle");
135   map.put("value", "127.0.0.1");
136   map.put("作用", "用来测试玩的,呵呵...");
137   setAllValue(map);
138   System.out.println(ConfigUtil.getString("姓名"));
139   HashMap<String ,String> map2 = getAll();
140   for(Map.Entry<String, String>m:map2.entrySet())
141   {
142    System.out.println(m.getKey()+" = "+m.getValue());
143   }
144  }
145 }
146  
原文地址:https://www.cnblogs.com/liwei45212/p/3028549.html