Java对Properties文件的操作

【说明】本篇文章为转载,文章来源 http://blog.csdn.net/shawearn1027  

简介

在 Java 中,我们常用 java.util.Properties.Properties 类来解析 Properties 文件,Properties 格式文件是 Java 常用的配置文件,它用来在文件中存储键-值对,其中键和值用等号分隔,格式如下:

  1. name=shawearn  

Properties 类是 java.util.Hashtable<Object, Object> 的子类,用于键和值之间的映射。

在对 Properties 格式文件的操作中,我们常使用 Properties 类的一下方法:

Properties():用于创建一个无任何属性值 Properties 对象;

  • void load(InputStream inStream):从输入流中加载属性列表;
  • void store(OutputStream out, String comments):根据输出流将属性列表保存到文件中; 
  • String  getProperty(String key):获取指定键的值;
  • void setProperty(String key, String value):设置指定键的值,若指定键已经在原属性值列表中存在,则覆盖;若指定键在原属性值列表中不存在,则新增;

写入 Properties 文件:

  1. // 创建一个 Properties 实例;  
  2. Properties p = new Properties();  
  3. // 为 Properties 设置属性及属性值;  
  4. p.setProperty("name""shawearn");  
  5. p.setProperty("address""XX 省 XX 市");  
  6. // 保存 Properties 到 shawearn.properties 文件中;  
  7. FileOutputStream out = new FileOutputStream("shawearn.properties");  
  8. p.store(out, "Create by Shawearn!");  
  9. out.close();  

读取 Properties 文件:

  1. // 创建一个 Properties 实例;  
  2. Properties p = new Properties();  
  3. // 读取配置文件;  
  4. FileInputStream in = new FileInputStream("shawearn.properties");  
  5. // 加载配置文件到 Properties 实例中;  
  6. p.load(in);  
  7. in.close();  

最后附上测试代码:

  1. package com.shawearn.test;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.util.Properties;  
  6. import java.util.Set;  
  7. /** 
  8.  * @author Shawearn 
  9.  *  
  10.  */  
  11. public class TestProperties {  
  12.     /** 
  13.      * @param args 
  14.      * @throws IOException 
  15.      */  
  16.     public static void main(String[] args) throws IOException {  
  17.         TestProperties t = new TestProperties();  
  18.         // 测试写入;  
  19.         t.testWrite();  
  20.         // 测试读取;  
  21.         t.testRead();  
  22.     }  
  23.     /* 
  24.      * 测试对 Properties 文件的写入操作; 
  25.      */  
  26.     private void testWrite() throws IOException {  
  27.         // 创建一个 Properties 实例;  
  28.         Properties p = new Properties();  
  29.         // 为 Properties 设置属性及属性值;  
  30.         p.setProperty("name""shawearn");  
  31.         p.setProperty("address""XX 省 XX 市");  
  32.         // 保存 Properties 到 shawearn.properties 文件中;  
  33.         FileOutputStream out = new FileOutputStream("shawearn.properties");  
  34.         p.store(out, "Create by Shawearn!");  
  35.         out.close();  
  36.         System.out.println("写入成功!");  
  37.     }  
  38.     /* 
  39.      * 测试对 Properties 文件的读取操作; 
  40.      */  
  41.     private void testRead() throws IOException {  
  42.         // 创建一个 Properties 实例;  
  43.         Properties p = new Properties();  
  44.         // 读取配置文件;  
  45.         FileInputStream in = new FileInputStream("shawearn.properties");  
  46.         // 加载配置文件到 Properties 实例中;  
  47.         p.load(in);  
  48.         in.close();  
  49.         // 获取 Properties 文件中所有的 key;  
  50.         Set<String> keys = p.stringPropertyNames();  
  51.         // 遍历所有的 key;  
  52.         for (String key : keys) {  
  53.             // 获取 Properties 文件中 key 所对应的 value;  
  54.             Object value = p.get(key);  
  55.             // 输入 key 和对应的 value;  
  56.             System.out.println(key + " => " + value);  
  57.         }  
  58.     }  
  59. }  
控制台输出结果:

  1. address => XX 省 XX 市  
  2. name => shawearn  
shawearn.properties 文件内容:

  1. #Create by Shawearn!  
  2. #Thu Nov 19 12:43:41 CST 2015  
  3. name=shawearn  
  4. address=XX \u7701 XX \u5E02  

猜你喜欢

转载自blog.csdn.net/start_mao/article/details/78721036
今日推荐