java获取properties类实例

时间:2020-05-08 23:26:26 类型:JAVA
字号:    

ClassPath根下config.properties文件内容

uploads=F:/java/uploads

读取properties类

package utils;

import java.io.*;
import java.util.Properties;

public class PropertiesGet {
    // 类名.class.getResourceAsStream(String path)
    //path 不以’/'开头时默认是从此类所在的包下取资源
    // 以’/'开头则是从ClassPath根下(即'/'代表src)获取
    public static Properties getResult(String proFileName) throws IOException {
        Properties props = new Properties();
        InputStream inputStream = PropertiesGet.class.getResourceAsStream(proFileName);
        //*.properties配置文件,要使用UTF-8编码,否则会现中文乱码问题
        BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
        props.load(bf);
        return  props;
    }
}

读取实例

Properties props = PropertiesGet.getResult("/config.properties");
String filePath = props.getProperty("uploads");
out.println(filePath);


<