读取properties文件,项目下和绝对路径的读取方法

 方法一:test.properties文件在项目里面的的src下

ResourceBundle resourceBundle = ResourceBundle.getBundle("test");
String name = resourceBundle.getString("name");
System.out.println(name);

遇到以下问题:

ResourceBundle resourceBundle = ResourceBundle.getBundle("test");//文件名为test.properties  我的是java工程在src下。

原因一:文件名不对(不加后缀 .properties。我就是这个问题,手残,找了很久)

原因二:配置文件路径不对,我写的是java工程,需要把配置文件src下,如果放在包下需增加路径(如:放在src/com包下是“com.test”)

方法二:使用IO流读取绝对路径文件,文件在E:/test.properties下

Properties properties = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream("E:/test.properties"));
properties.load(in);
properties.getProperty("name");
System.out.println(properties.getProperty("name"));

猜你喜欢

转载自blog.csdn.net/leyili_s/article/details/80853140