ClassPath根下config.properties文件內(nèi)容
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編碼,否則會現(xiàn)中文亂碼問題
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);