1.java.util.ResourceBundle通过ResourceBundle对象操作
此方法值可用来读取文件,无法操作修改配置文件
@Test
public void readTest(){
ResourceBundle resource = ResourceBundle.getBundle("context");//配置文件相对工程根目录的相对路径(不含扩展名)
String key = resource.getString("url");
System.out.println(key);
}
2.java.util.Properties通过Properties对象操作
@Test
public void readTestProperties(){
Properties pt = new Properties();
try {
//获取相对路径下的配置文件(通过类加载器获取对应根目录下的resource资源)
InputStream read = this.getClass().getClassLoader().getResourceAsStream("context.properties");
//获取绝对路径下的配置文件,可获取任意(以下两种方式均可)
// BufferedReader read = new BufferedReader(new FileReader("C:/iMold/workspace/htmpro/src/main/resources/context.properties"));
// FileInputStream read = new FileInputStream("C:/iMold/workspace/htmpro/src/main/resources/context.properties");
pt.load(read);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("username="+ pt.getProperty("username") + "\n" + "pwd=" + pt.getProperty("pwd"));
}