加载文件的两种方式

1,文件在具体的包下

ClassLoader loader = this.getClass().getClassLoader();
InputStream is = loader.getResourceAsStream("com\atguigu\java\jdbc.properties");

2,文件在当前工程下

FileInputStream is = new FileInputStream(new File("jdbc1.properties"));

如下图:


//法一:
ClassLoader loader = this.getClass().getClassLoader();
InputStream is = loader.getResourceAsStream("com\atguigu\java\jdbc.properties");
//法二:
// FileInputStream is = new FileInputStream(new File("jdbc1.properties"));

Properties pros = new Properties();
pros.load(is);
String name = pros.getProperty("user");
System.out.println(name);

String password = pros.getProperty("password");
System.out.println(password);

纸上学来终觉浅,觉知此事需躬行
原文地址:https://www.cnblogs.com/dreamHighMjc/p/7446222.html