Java中Properties类,为什么用Properties类

要求:配置一个mysql1.properties的文件,其中包括

ip=192.168.10.13
user=root
pwd=111111

传统方法:

代码:

public class Properties1 {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("src\\main\\java\\com\\example\\retcode\\test\\file\\properties\\properties1\\mysql1.properties"));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            String[] split = line.split("=");
            //if(split[0].equals("ip"))//只读ip
            System.out.println(split[0] + "是:" + split[1]);
        }
    }

}

使用Properties类:

代码:

public class Properties1 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileReader("src\\main\\java\\com\\example\\retcode\\test\\file\\properties\\properties1\\mysql1.properties"));
        properties.list(System.out);
    }
}

Properties类常用方法:

代码:

public class PropertiesMethods {
    public static void main(String[] args) throws IOException {
        //使用 Properties 类来读取 mysql.properties 文件
        //1. 创建 Properties 对象
        Properties properties = new Properties();
        //2. 加载指定配置文件
        properties.load(new FileReader("src\\main\\java\\com\\example\\retcode\\test\\file\\properties\\properties1\\mysql1.properties"));
        //3. 把 k-v 显示控制台
        properties.list(System.out);
        //4. 根据 key 获取对应的值
        String user = properties.getProperty("user");
        String pwd = properties.getProperty("pwd");
        System.out.println("用户名=" + user);
        System.out.println("密码是=" + pwd);
    }
}

使用 Properties 类来创建 配置文件, 修改配置文件内容:

代码:

public class Properties2 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();

        //创建
        //1.如果该文件没有 key 就是创建
        //2.如果该文件有 key ,就是修改
        /*
        修改源码:
        Properties 父类是 Hashtable ,底层就是 Hashtable 核心方法
        public synchronized V put (K key, V value){
        // Make sure the value is not null
            if (value == null) {
                throw new NullPointerException();
            }
        // Makes sure the key is not already in the hashtable. Entry tab[] = table;
            int hash = key.hashCode();
            int index = (hash & 0x7FFFFFFF) % tab.length;
            @SuppressWarnings("unchecked")
            Entry entry = (Entry) tab[index];
            for (; entry != null; entry = entry.next) {
                if ((entry.hash == hash) && entry.key.equals(key)) {
                    V old = entry.value;
                    entry.value = value;//如果 key 存在,就替换
                    return old;
                }
            }
            addEntry(hash, key, value, index);//如果是新 k, 就 addEntry
            return null;
        }
        */

        properties.setProperty("ip", "192.168.10.13");
        properties.setProperty("user", "root");//注意保存时,是中文的 unicode 码值
        properties.setProperty("pwd", "111111");
        properties.store(new FileWriter("src\\main\\java\\com\\example\\retcode\\test\\file\\properties\\properties2\\mysql2.properties"), null);//null表示没有注解

    }
}

 

你可能感兴趣的:(java,开发语言)