在数据库连接时,读取配置文件时遇到的各种异常(java.io.FileNotFoundException: src\jdbc.properties (系统找不到指定的文件。)和java.lang.NullPointerException: inStream parameter is null),一网打尽:
public class ClassLoaderTest {
@Test
//使用ClassLoader加载配置文件:
public void test2() throws IOException {
Properties pros = new Properties();
//此时的jdbc.properties文件默认在当前的module下
//读取配置文件的方式一:
//FileInputStream fis = new FileInputStream("jdbc.properties");
FileInputStream fis = new FileInputStream("src\\jdbc1.properties");
pros.load(fis);
//读取配置文件的方式二:使用ClassLoader
//配置文件默认识别为当前module的src下
/*ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("jdbc1.properties");
pros.load(is);*/
String user = pros.getProperty("user");
String password = pros.getProperty("pas");
System.out.println(user);
System.out.println(password);
}
}