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

IOUtils.toString(inputStream);
IOUtils.write(s, new FileOutputStream("d:/jdbc2.txt"));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM