Java Properties 类读取和修改配置文件信息

来源:互联网 发布:json高亮js 编辑:程序博客网 时间:2024/06/07 19:36
  1. import java.io.BufferedInputStream;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.util.Properties;  
  9.   
  10. /** 
  11.  * Java读写修改Property文件 
  12.  * @author xiewanzhi 
  13.  * @date 2011-4-7上午09:19:03 
  14.  * @version 1.0 
  15.  */  
  16. public class PropertiesConfig {  
  17.   
  18.     /** 
  19.      * 根据KEY,读取文件对应的值 
  20.      * @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties 
  21.      * @param key 键 
  22.      * @return key对应的值 
  23.      */  
  24.     public static String readData(String filePath, String key) {  
  25.         //获取绝对路径  
  26.         filePath = PropertiesConfig.class.getResource("/" + filePath).toString();  
  27.         //截掉路径的”file:“前缀  
  28.         filePath = filePath.substring(6);  
  29.         Properties props = new Properties();  
  30.         try {  
  31.             InputStream in = new BufferedInputStream(new FileInputStream(filePath));  
  32.             props.load(in);  
  33.             in.close();  
  34.             String value = props.getProperty(key);  
  35.             return value;  
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.             return null;  
  39.         }  
  40.     }  
  41.     /** 
  42.      * 修改或添加键值对 如果key存在,修改, 反之,添加。 
  43.      * @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties 
  44.      * @param key 键 
  45.      * @param value 键对应的值 
  46.      */  
  47.     public static void writeData(String filePath, String key, String value) {  
  48.         //获取绝对路径  
  49.         filePath = PropertiesConfig.class.getResource("/" + filePath).toString();  
  50.         //截掉路径的”file:/“前缀  
  51.         filePath = filePath.substring(6);  
  52.         Properties prop = new Properties();  
  53.         try {  
  54.             File file = new File(filePath);  
  55.             if (!file.exists())  
  56.                 file.createNewFile();  
  57.             InputStream fis = new FileInputStream(file);  
  58.             prop.load(fis);  
  59.             //一定要在修改值之前关闭fis  
  60.             fis.close();  
  61.             OutputStream fos = new FileOutputStream(filePath);  
  62.             prop.setProperty(key, value);  
  63.             //保存,并加入注释  
  64.             prop.store(fos, "Update '" + key + "' value");  
  65.             fos.close();  
  66.         } catch (IOException e) {  
  67.             System.err.println("Visit " + filePath + " for updating " + value + " value error");  
  68.         }  
  69.     }  
  70.       
  71.     public static void main(String[] args) {  
  72.         System.out.println(PropertiesConfig.readData("com/xiewanzhi/property/config.properties""port"));  
  73. //      PropertiesConfig.writeData("com/xiewanzhi/property/config.properties", "port", "12345");  
  74.     }  
  75. }  
    来源: <http://zhizaibide1987.iteye.com/blog/1014308>
     


properties的配置添加、删除、修改操作

    博客分类: 
  • j2ee
  • jsp/java
  • Java.Util
 
Java代码 复制代码 收藏代码
  1. public void saveProperties()  
  2.     {  
  3.         try  
  4.         {  
  5.             Properties properties = new Properties();  
  6.   
  7.             Properties p = new Properties();  
  8.             File file = new File("F://test.properties");  
  9.             p.load(new FileInputStream(file));  
  10.             Set<String> pSet = p.stringPropertyNames();  
  11.             Iterator i = pSet.iterator();  
  12.             while(i.hasNext())  
  13.             {  
  14.                 String propertiesName = i.next().toString();  
  15.                 //删除一个当获取的名称hk相同时,就返回到下一步;break;是退出循环  
  16.                 if("hk".equalsIgnoreCase(propertiesName)) continue;  
  17.                 properties.setProperty(propertiesName, p.getProperty(propertiesName));  
  18.                 //修改  
  19.                 if("japan".equalsIgnoreCase(propertiesName))  
  20.                 {  
  21.                     properties.setProperty(propertiesName, "123456789");  
  22.                 }  
  23.             }  
  24.             properties.setProperty("usa""美国");      
  25.             properties.setProperty("hk""香港");   
  26.             properties.setProperty("japan""日本");                
  27.             properties.setProperty("china""中国");    
  28.                                                 //添加              
  29.             properties.store(new FileOutputStream(file), properties.toString());  
  30.               
  31.         } catch (IOException e)  
  32.         {  
  33.             e.printStackTrace();  
  34.         }  
  35.     }  
     

原创粉丝点击