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

  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. }  
     


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.     }  
     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值