java.util.Properties

ava.util.Properties

   原创,转发请注明出处

  类定义:

  从下面的类定义可以知道Properties 类本质上是一个Hashtable,可存放key-value键值对。但是作为工具类它提供额外的功能实现,可以通过.properties、.txt等文件形式获取key-value。

  public class Properties extends Hashtable<Object,Object>{}

  参数:

  除了serialVersionUID外,该工具类只有一个参数。

  protected Properties defaults;

  构造方法:

  /**
     * Creates an empty property list with no default values.
     */
  public Properties() {
    this(null);
  }
  /**
     * Creates an empty property list with the specified defaults.
     *
     * @param   defaults   the defaults.
     */
  public Properties(Properties defaults) {
    this.defaults = defaults;
  }

  主要方法:

  //新加一组key-value
  public
synchronized Object setProperty(String key, String value) {     return put(key, value);   }  
  //使用字符流加载Properties
  public
synchronized void load(Reader reader) throws IOException {   }
  //使用字符流加载Properties
  public synchronized void load(Reader reader) throws IOException {
  } 
  //将Properties通过输出流写出,该方法已过时,不推荐使用
    public void save(OutputStream out, String comments)  {
    }  
  //将property list 通过字符流输出
  public
void store(Writer writer, String comments)throws IOException{ }
  
  //将property list通过字节流输出
  public
void store(OutputStream out, String comments)throws IOException{ }
  //使用XML文件输入流加载所有properties
  public
synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException{ }
  //将properties list通过输出流写出到xml文件
  public void storeToXML(OutputStream os, String comment)throws IOException{ }
  //写出到xml文件并设置注释和编码方式
  public void storeToXML(OutputStream os, String comment, String encoding) throws IOException{
    }
    //根据key获取对应的property
    public String getProperty(String key) {       
    }
    //使用指定的key获取properties,如果指定的key不存在,则可以使用第二个参数指定默认值
    public String getProperty(String key, String defaultValue) {
        String val = getProperty(key);
        return (val == null) ? defaultValue : val;
    }
   //返回属性名的枚举 
    public Enumeration<?> propertyNames() {
        Hashtable h = new Hashtable();
        enumerate(h);
        return h.keys();
    }
   // 返回属性的的set集合
    public Set<String> stringPropertyNames() {
    }
    // 将property list写出到指定的输出流(字节),这个方法在调试时很有用
    public void list(PrintStream out) {
        ...
        out.println(key + "=" + val);
    }
    // 将property list写出到指定的输出流(字符),这个方法在调试时很有用
    public void list(PrintWriter out) {
        ...
        out.println(key + "=" + val);
    }

  实例

  从test.txt文件中获取信息

  测试代码

public class PropertiesTest {
    public static void main(String[] args) {
    try {
    //获取文件输入流
        InputStream is = new FileInputStream("C:/Users/HOME/Desktop/test.txt");
        Properties prop = new Properties();
    //通过输入流加载property list
        prop.load(is);
    //原本有5对测试键值对,新加一个
        prop.setProperty("new", "test");
    //输出属性组的个数,此时应该为6
        System.out.println("属性组数量:"+prop.size());
        String value = "";
    //遍历输出keys 和 values
for (Object pro : prop.keySet()) { value = prop.getProperty((String) pro); System.out.print((String) pro+"---"); System.out.println(value); }
    //对于不存在的key,设置默认值     System.out.println(prop.getProperty(
"none","default"));
    //将property list 写出到指定的xml文件中
    prop.storeToXML(new FileOutputStream("C:/Users/HOME/Desktop/test.xml"), "TOXML注释"); }
catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

  结果预览

 

  将property list写出到xml文件中

   

posted @ 2018-01-15 11:14  maxudong  阅读(720)  评论(0编辑  收藏  举报