java读取项目外的配置文件
我将配置文件filepath.properties直接放到了C盘下
配置文件内容如下:
# 注释用#号
systemDefaultDownloadPath=C:\\Users\\Administrator\\Downloads
# 有多个值要另起一行,不要加,;一些符号,等号两边不要加空格
linuxFilePath=java/Downloads
然后java读取配置文件,代码如下:
import java.io.*;
import java.util.Properties;
public class test {
public static void main(String[] args) {
//传入key值
String filePath = loadProps("systemDefaultDownloadPath");
System.out.println(filePath);
}
public static String loadProps(String filePathName) {
Properties props = new Properties();
InputStream in = null;
try {
//读取配置文件
in =new BufferedInputStream(new FileInputStream("C:\\filepath.properties"));
props.load(in);
} catch (Exception e) {
System.out.println("读取jdbc.properties时出现异常:" + e);
} finally {
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
System.out.println("jdbc.properties文件流关闭出现异常");
}
}
//通过key值找value值
String filePath = props.getProperty(filePathName);
return filePath;
}
}