使用Properties來讀取配置檔案
阿新 • • 發佈:2020-12-08
技術標籤:JavaCore
使用Properties讀取的檔案內容
/*
Properties:用來讀取配置檔案
*/
@Test
public void test2() throws IOException {
Properties properties=new Properties();
//此時的檔案預設在當前餓module下。
//讀取配置檔案的方式一:
// FileInputStream fileInputStream = new FileInputStream("jdbc.properties");
// FileInputStream fileInputStream = new FileInputStream("src\\jdbc1.properties");
// properties.load(fileInputStream);
//讀取配置檔案的方式二:使用ClassLoader
//配置檔案預設識別為:當前module下的src下
ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
InputStream resourceAsStream = classLoader.getResourceAsStream("jdbc1.properties");
properties.load(resourceAsStream);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
System.out.println("user="+user+",password=" +password);
}