java对properties配置文件的读写操作

1.1.  对properties配置文件的读取

1.1.1.  要读取的文件

配置文件keyvalue之间用冒号[:]和等于号[=]都是可以的.

Test.properties

name:\u5F20\u4E09

password:\u5BC6\u7801

age:22

1.1.2.  读取的程序代码

PropertiesTest.Java

[java]  view plain  copy
  1. /** 
  2.      * 读取配置文件 
  3.      * @throws IOException 
  4.      */  
  5.     @Test  
  6.     public void read() throws IOException{  
  7.        Properties properties = new Properties();  
  8.         
  9.        File file = new File("test.properties");  
  10.         
  11.        FileInputStream fis = new FileInputStream(file);  
  12.         
  13.        properties.load(fis);  
  14.         
  15.        Set<Object> keySet = properties.keySet();  
  16.         
  17.        for (Object object : keySet) {  
  18.            System.out.println(object.toString()+":"+properties.getProperty(object.toString()));  
  19.        }  
  20.     }  

1.1.3.  结果

后台打印结果

age:22

password:密码

name:张三

1.2.  对propertiest配置文件的写入操作

1.2.1.  写入程序的代码

PropertiesTest.java

[java]  view plain  copy
  1. /** 
  2.      * 向配置文件写入内容 
  3.      * @throws IOException 
  4.      */  
  5.     @Test  
  6.     public void write() throws IOException {  
  7.        Properties properties = new Properties();  
  8.         
  9.        String file = "aa.properties";  
  10.        FileOutputStream fos = new FileOutputStream(file);  
  11.         
  12.        properties.setProperty("aa""123");  
  13.        properties.setProperty("bb""456");  
  14.        properties.setProperty("cc""789");  
  15.        properties.setProperty("name""张三");  
  16.         
  17.         
  18.        properties.store(fos, "保存文件");  
  19.         
  20.        fos.close();  
  21.     }  

1.2.2.  结果

aa.propertiest

#\u4FDD\u5B58\u6587\u4EF6

#Sun Jan 25 14:06:25 CST 2015

name=\u5F20\u4E09

aa=123

bb=456

cc=789

1.3.  对properties配置文件的修改操作

1.3.1.  修改前的配置文件

Test.properties

Age=22

Password=密码

Name=张三

1.3.2.  修改程序的代码

PropertiesTest.java

  

[java]  view plain  copy
  1. /** 
  2.    * 对配置文件的修改 
  3.    * @throws IOException 
  4.    */  
  5.   @Test  
  6.   public void update() throws IOException{  
  7.       
  8.      Properties properties = new Properties();  
  9.       
  10.      File file = new File("test.properties");  
  11.       
  12.      FileInputStream fis = new FileInputStream(file);  
  13.       
  14.      FileOutputStream fos = new FileOutputStream(file);  
  15.       
  16.      properties.load(fis);  
  17.       
  18.      properties.setProperty("name""李四");  
  19.      properties.setProperty("age""222");  
  20.      properties.setProperty("age3""222");  
  21.      properties.store(fos, "更新配置文件");  
  22.       
  23.      fos.close();  
  24.       
  25.      fis.close();  
  26.       
  27.   }  

1.3.3.  修改后的文件内容

Test.properties

#\u66F4\u65B0\u914D\u7F6E\u6587\u4EF6

#Sun Jan 25 15:20:40 CST 2015

age=222

name=\u674E\u56DB

password=\u5BC6\u7801

age3=222

1.4.  对properties配置文件的删除

1.4.1.  删除前配置文件的内容

Test.properties

#\u66F4\u65B0\u914D\u7F6E\u6587\u4EF6

#Sun Jan 25 15:20:40 CST 2015

age=222

name=\u674E\u56DB

password=\u5BC6\u7801

age3=222

1.4.2.  删除程序的代码

PropertiesTest.java

[java]  view plain  copy
  1. /** 
  2.      * 删除配置文件中的key 
  3.      * @throws IOException 
  4.      * 
  5.      */  
  6.     @Test  
  7.     public void delete() throws IOException{  
  8.        Properties properties = new Properties();  
  9.         
  10.        File file = new File("test.properties");  
  11.         
  12.        FileInputStream fis = new FileInputStream(file);  
  13.                
  14.        properties.load(fis);     
  15.         
  16.        //必须先用map将所有的内容先保存,不然一删除,原来的内容都没了  
  17.        Map<String, String> map = new HashMap<String, String>();  
  18.         
  19.        Set<Object> keySet = properties.keySet();  
  20.         
  21.        System.out.println(keySet.size());  
  22.         
  23.        for (Object object : keySet) {  
  24.            String key = (String) object;  
  25.            String value = (String) properties.get(key);  
  26.            System.out.println(key+"="+value);  
  27.            map.put(key, value);  
  28.        }  
  29.        //删除key  
  30.        map.remove("age3");  
  31.        properties.remove("age3");  
  32.         
  33.        for (java.util.Map.Entry<String, String> entry: map.entrySet()) {  
  34.            properties.setProperty(entry.getKey(), entry.getValue());  
  35.        }  
  36.         
  37.        FileOutputStream fos = new FileOutputStream(file);  
  38.        properties.store(fos, "删除配置文件中的key:age3");       
  39.    
  40.        fos.close();  
  41.         
  42.        fis.close();  
  43.         
  44.     }    

1.4.3.  删除后的配置文件内容

Test.properties

#\u5220\u9664\u914D\u7F6E\u6587\u4EF6\u4E2D\u7684key:age3

#Sun Jan 25 15:23:58 CST 2015

age=222

name=\u674E\u56DB

password=\u5BC6\u7801

1.5.  完整程序代码

PropertieTest.java

[java]  view plain  copy
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7. import java.util.Properties;  
  8. import java.util.Set;  
  9.    
  10. import org.junit.Test;  
  11.       
  12. public class PropertiesTest {  
  13.    
  14.     public static void main(String[] args) {  
  15.    
  16.     }  
  17.     /** 
  18.      * 删除配置文件中的key 
  19.      * @throws IOException 
  20.      * 
  21.      */  
  22.     @Test  
  23.     public void delete() throws IOException{  
  24.        Properties properties = new Properties();  
  25.         
  26.        File file = new File("test.properties");  
  27.         
  28.        FileInputStream fis = new FileInputStream(file);  
  29.                
  30.        properties.load(fis);     
  31.         
  32.        //必须先用map将所有的内容先保存,不然一删除,原来的内容都没了  
  33.        Map<String, String> map = new HashMap<String, String>();  
  34.         
  35.        Set<Object> keySet = properties.keySet();  
  36.         
  37.        System.out.println(keySet.size());  
  38.         
  39.        for (Object object : keySet) {  
  40.            String key = (String) object;  
  41.            String value = (String) properties.get(key);  
  42.            System.out.println(key+"="+value);  
  43.            map.put(key, value);  
  44.        }  
  45.        //删除key  
  46.        map.remove("age3");  
  47.        properties.remove("age3");  
  48.         
  49.        for (java.util.Map.Entry<String, String> entry: map.entrySet()) {  
  50.            properties.setProperty(entry.getKey(), entry.getValue());  
  51.        }  
  52.         
  53.        FileOutputStream fos = new FileOutputStream(file);  
  54.        properties.store(fos, "删除配置文件中的key:age3");       
  55.    
  56.        fos.close();  
  57.         
  58.        fis.close();  
  59.         
  60.     }  
  61.      
  62.     /** 
  63.      * 对配置文件的修改 
  64.      * @throws IOException 
  65.      */  
  66.     @Test  
  67.     public void update() throws IOException{  
  68.         
  69.        Properties properties = new Properties();  
  70.         
  71.        File file = new File("test.properties");  
  72.         
  73.        FileInputStream fis = new FileInputStream(file);  
  74.         
  75.         
  76.         
  77.        properties.load(fis);  
  78.         
  79.        //必须先用map将所有的内容先保存,不然一更新,原来的内容都没了  
  80.        Map<String, String> map = new HashMap<String, String>();  
  81.         
  82.        Set<Object> keySet = properties.keySet();  
  83.         
  84.        System.out.println(keySet.size());  
  85.         
  86.        for (Object object : keySet) {  
  87.            String key = (String) object;  
  88.            String value = (String) properties.get(key);  
  89.            System.out.println(key+"="+value);  
  90.            map.put(key, value);  
  91.        }  
  92.         
  93.        map.put("name""李四");  
  94.        map.put("age""222");  
  95.        map.put("age3""222");  
  96.         
  97.         
  98.        for (java.util.Map.Entry<String, String> entry: map.entrySet()) {  
  99.            properties.setProperty(entry.getKey(), entry.getValue());  
  100.        }  
  101.        FileOutputStream fos = new FileOutputStream(file);  
  102.         
  103.        properties.store(fos, "更新配置文件");  
  104.         
  105.        fos.close();  
  106.         
  107.        fis.close();  
  108.         
  109.     }  
  110.      
  111.      
  112.     /** 
  113.      * 向配置文件写入内容 
  114.      * @throws IOException 
  115.      */  
  116.     @Test  
  117.     public void write() throws IOException {  
  118.        Properties properties = new Properties();  
  119.         
  120.        String file = "aa.properties";  
  121.        FileOutputStream fos = new FileOutputStream(file);  
  122.         
  123.        properties.setProperty("aa""123");  
  124.        properties.setProperty("bb""456");  
  125.         properties.setProperty("cc""789");  
  126.        properties.setProperty("name""张三");  
  127.         
  128.         
  129.        properties.store(fos, "保存文件");  
  130.         
  131.        fos.close();  
  132.     }  
  133.      
  134.      
  135.     /** 
  136.      * 读取配置文件 
  137.      * @throws IOException 
  138.      */  
  139.     @Test  
  140.     public void read() throws IOException{  
  141.        Properties properties = new Properties();  
  142.         
  143.        File file = new File("test.properties");  
  144.         
  145.        FileInputStream fis = new FileInputStream(file);  
  146.         
  147.        properties.load(fis);  
  148.         
  149.        Set<Object> keySet = properties.keySet();  
  150.         
  151.        for (Object object : keySet) {  
  152.            System.out.println(object.toString()+"="+properties.getProperty(object.toString()));  
  153.        }  
  154.     }  
  155.    
  156. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值