问题:
//1.加载配置文件
Properties pro = new Properties();
//使用ClassLoader加载配置文件,获取字节输入流
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
将配置文件放在src目录下,启动程序后,获取不到配置文件链接,会提示 "inStream parameter is null "
通过搜索,找到解决方案
解决方案:
将配置文件放在resources文件夹下。
原因:
使用ClassLoder读取配置文件,该方式只能读取类路径下的配置文件,所以一般需要将配置文件放在类路径目录下,即resources下。
拓展:
基于 InputStream 读取配置文件,该方式的优点在于可以读取任意路径下的配置文件。
1 Properties pro = new Properties();
2
3 // 使用InPutStream流读取properties文件
4 BufferedReader bufferedReader = new BufferedReader(new
5 FileReader("config.properties"));
6 pro.load(bufferedReader);
7
8 // 获取key对应的value值
9 pro.getProperty(String key);
在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定,如:config.properties若在com.test.config包下,则要使用com/test/config/config.properties(通过Properties来获取);属性文件在src根目录下,则直接使用config.properties或config即可。
刚刚接触Java,学习过程中用csdn来记录一下。