java.util.Properties的使用及读取资源文件

1、工具类Utils

package com.oy.utils;

import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author oy
 * @date 2019年6月9日 下午7:20:33
 * @version 1.0.0
 */
public class Utils {

    // 根据资源文件的绝对路径获取Properties
    public static Properties getPropertiesByFilePath(String path) {
        Properties properties = new Properties();
        InputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(path));
            properties.load(bis);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(bis);
        }
        return properties;
    }

    // 使用ClassLoader来获取类路径下的资源
    // 资源路径path: 前面没有"/", 相对于classpath目录
    public static Properties getPropertiesByClassLoader(String path) {
        Properties properties = new Properties();
        InputStream is = Utils.class.getClassLoader().getResourceAsStream(path);
        try {
            properties.load(is);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(is);
        }
        return properties;
    }

    // 使用Class来获取类路径下的资源
    // 资源路径path: 以"/"开头, 相对于classpath目录; 不以"/"开头, 相对当前.class文件所在目录
    public static Properties getPropertiesByClass(String path) {
        Properties properties = new Properties();
        InputStream is = Utils.class.getResourceAsStream(path);
        try {
            properties.load(is);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(is);
        }
        return properties;
    }

    // 使用ClassLoader来获取类路径下的资源
    // 资源路径path: 前面没有"/", 相对于classpath目录
    public static InputStream getInputStreamByClassLoader(String path) {
        InputStream is = Utils.class.getClassLoader().getResourceAsStream(path);
        return is;
    }

    // 使用Class来获取类路径下的资源
    // 资源路径path: 以"/"开头, 相对于classpath目录; 不以"/"开头, 相对当前.class文件所在目录
    public static InputStream getInputStreamByClass(String path) {
        InputStream is = Utils.class.getResourceAsStream(path);
        return is;
    }

    // 关闭io流对象
    public static void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

2、在src下新建jdbc.properties

driverClass = com.mysql.jdbc.Driver  
jdbcURL = jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=utf-8   
username = root  
password = root  
name=张三

3、测试

package com.oy.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import com.oy.utils.Utils;

/**
 * @author oy
 * @date 2019年6月9日 下午7:15:49
 * @version 1.0.0
 */
public class Demo01 {

    @Test
    public void test1() {
        Properties pps = System.getProperties();
        pps.list(System.out);
    }

    @Test
    public void test2() {
        Properties properties = Utils.getPropertiesByClassLoader("jdbc.properties");
        // properties.list(System.out);
        
        // 遍历
        for (Entry<Object, Object> entry : properties.entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
        
        // 遍历
        Enumeration en = properties.propertyNames(); 
        while(en.hasMoreElements()) {
            String strKey = (String) en.nextElement();
            String strValue = properties.getProperty(strKey);
            System.out.println(strKey + "=" + strValue);
        }
        
        // getProperty: 根据key获取value
        System.out.println("username=" + properties.getProperty("username"));
        System.out.println("password=" + properties.getProperty("password"));
        
        // setProperty: 根据key修改value
        properties.setProperty("password", "root001");
        
        // properties.clear(); 删除所有键值对
        
        try {
            properties.store(new FileOutputStream("d:/jdbc.txt"), "数据库连接配置");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test3() {
        InputStream inputStream = Utils.getInputStreamByClassLoader("jdbc.properties");
        String s = "";
        try {
            // 读取输入流,转换成字符串
            s = IOUtils.toString(inputStream);
            System.out.println(s);
            
            // 字符串写入到文件
            IOUtils.write(s, new FileOutputStream("d:/jdbc2.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  结果 jdbc.txt

#\u6570\u636E\u5E93\u8FDE\u63A5\u914D\u7F6E
#Sun Jun 09 23:31:43 CST 2019
driverClass=com.mysql.jdbc.Driver  
name=\u00E5\u00BC\u00A0\u00E4\u00B8\u0089
password=root001
jdbcURL=jdbc\:mysql\://127.0.0.1\:3306/db_test?useUnicode\=true&characterEncoding\=utf-8   
username=root  

  jdbc2.txt

driverClass = com.mysql.jdbc.Driver  
jdbcURL = jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=utf-8   
username = root  
password = root  
name=张三

4、其他

  上面代码中使用了commons-io-2.5.jar

猜你喜欢

转载自www.cnblogs.com/xy-ouyang/p/10995455.html