项目目录结构:
ClassLoader
properties文件只能在类目录下,也就是对应于这里src的相对路径
Properties p1 = new Properties();
InputStream inputStream = Program.class.getClassLoader()
.getResourceAsStream("com/zrun/app1.properties");
// .getResourceAsStream("config/app2.properties");
// .getResourceAsStream("app3.properties");
p1.load(inputStream);
System.out.println(p1.getProperty("appid"));
BufferedReader
实际上就是从绝对路径加载文件流,配置文件在任何地方都可以,不一定要在类目录下
Properties properties = new Properties();
BufferedReader bufferedReader = new BufferedReader(
new FileReader(
"E:\08-javadev\TestReadPropertiesFile\config\app4.properties"));
InputStream inStream = new FileInputStream(
new File(
"E:\08-javadev\TestReadPropertiesFile\config\app4.properties"));
properties.load(bufferedReader);
// properties.load(inStream);
System.out.println(properties.getProperty("appid"));
ResourceBundle
properties文件只能在类目录下,也就是对应于这里src的相对路径
ResourceBundle resource = ResourceBundle.getBundle("com/zrun/app1");
// .getBundle("config/app2");
// .getBundle("app3");
System.out.println(resource.getString("appid"));