美文网首页
java.util.Properties

java.util.Properties

作者: Demon_aac4 | 来源:发表于2017-12-25 19:41 被阅读0次

    java.util.Properties 继承了 java.util.Hashtable<Object, Object>
    基本用法是new Properties()再调用load方法读取相应的配置文件

    package com.demon.test.testProperties;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Properties;
    
    public class Demo {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            Properties properties = new Properties();
    //      properties.load(inStream);  //调用load(InputStream inStream);//从流中读入配置文件
    //      properties.load(reader);    //调用load(Reader reader);//从流中读入配置文件
    //      properties.loadFromXML(in); //调用loadFromXML(InputStream in)//从流中读入xml格式配置文件
    //      properties.store(out, comments);    //调用store(OutputStream out, String comments)//输出配置文件到流
            
            properties.load(new FileInputStream("properties_InputStream"));
            properties.store(System.out, "show this properties  1");
            
            properties.load(new  FileReader("properties_Reader"));
            properties.store(System.out, "show this properties  2");
            
            properties.loadFromXML(new FileInputStream("XML_properties_InputStream"));
            properties.store(System.out, "show this properties  3");
        }
    }
    

    输出如下:

    #show this properties  1
    #Mon Dec 25 17:52:12 CST 2017
    name=demon
    #show this properties  2
    #Mon Dec 25 17:52:12 CST 2017
    age=123
    name=demon
    #show this properties  3
    #Mon Dec 25 17:52:12 CST 2017
    age=123
    name=demon.li
    key=value
    

    读入的三个配置文件内容为:
    properties_InputStream:

    name = demon
    

    properties_Reader

    name = demon
    age = 123
    

    XML_properties_InputStream

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    <properties>
        <entry key="name">demon.li</entry>
        <entry key="key">value</entry>
    </properties>
    

    1.输入

    Properties 从流中读入配置时调用的三个方法:

        public synchronized void load(Reader reader) throws IOException {
            load0(new LineReader(reader));
        }
        public synchronized void load(InputStream inStream) throws IOException {
            load0(new LineReader(inStream));
        }
        public synchronized void loadFromXML(InputStream in)
            throws IOException, InvalidPropertiesFormatException
        {
            XmlSupport.load(this, Objects.requireNonNull(in));
            in.close();
        }
    

    可以看到,load(Reader reader)load(InputStream inStream)是为load0 (LineReader lr)方法做了代理,分析load(InputStream inStream)方法和Properties$LineReader内部类,可以看到,load0 (LineReader lr)调用了lr.readLine()方法从流中解析出一行有效的配置,再解析成对应的key和value,调用Object java.util.Hashtable.put(Object key, Object value)将解析出来的key-value添加到自身(Hashtable)中

    //java.util.Properties.load0(LineReader)
    String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
    String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf);
    put(key, value);
    

    loadFromXML(InputStream in)方法调用了java.util.Properties.XmlSupport.load(Properties, InputStream)最终调用的是sun.util.spi.XmlPropertiesProvider.load(Properties arg0, InputStream arg1)方法,相对复杂但是从使用效果上看,和load是保持一致的,最后还是会将解析出来的key-value添加到自身去。
    这种设计可以同时load多个配置文件,先load的配置有可能被覆盖
    要修改或增加配置,最好调用java.util.Properties.setProperty(String, String)方法,即使它只是做了代理

        public synchronized Object setProperty(String key, String value) {
            return put(key, value);
        }
    

    2.输出

    Properties提供了list、save、store一系列方法帮助我们将配置写入输出流中,具体方法请alt+/
    其中java.util.Properties.save(OutputStream, String)方法已废弃,可以看到它代理了java.util.Properties.store(OutputStream, String)方法,我们使用时直接调用store系列方法即可
    java.util.Properties.store(OutputStream, String)java.util.Properties.store(Writer, String)方法是java.util.Properties.store0(BufferedWriter, String, boolean)方法的代理,从源码可以看出该方法遍历打印了所有的key-value,list方法也大同小异

        private void store0(BufferedWriter bw, String comments, boolean escUnicode)
            throws IOException
        {
            if (comments != null) {
                writeComments(bw, comments);
            }
            bw.write("#" + new Date().toString());
            bw.newLine();
            synchronized (this) {
                for (Enumeration<?> e = keys(); e.hasMoreElements();) {
                    String key = (String)e.nextElement();
                    String val = (String)get(key);
                    key = saveConvert(key, true, escUnicode);
                    /* No need to escape embedded and trailing spaces for value, hence
                     * pass false to flag.
                     */
                    val = saveConvert(val, false, escUnicode);
                    bw.write(key + "=" + val);
                    bw.newLine();
                }
            }
            bw.flush();
        }
    

    java.util.Properties.storeToXML(OutputStream, String)java.util.Properties.storeToXML(OutputStream, String, String)这两个方法和java.util.Properties.loadFromXML(InputStream)方法一样,最后最为调用的是sun.util.spi.XmlPropertiesProvider.store(Properties arg0, OutputStream arg1, String arg2, String arg3)方法,效果和java.util.Properties.store(OutputStream, String)java.util.Properties.store(Writer, String)方法保持一致。

    两个list方法和两个store方法类似。

    补充:需要注意,properties.load进去后,输入流不会被关闭,在读取完配置文件后,应该手动关闭掉输入流

    相关文章

      网友评论

          本文标题:java.util.Properties

          本文链接:https://www.haomeiwen.com/subject/tyzlgxtx.html