ClassLoader.getResource()出现异常
2019-06-12 本文已影响0人
面糊
-
通过获取绝对路径的方法,
properties.load()方法报文件找不到的异常, 使用代码如下:URL resource = classLoader.getResource("jdbc.properties"); String path = resource.getPath(); properties.load(new FileReader(path));- 文件路径为:
/Users/MyMac/Desktop/Java/02.%20JDBC/day01JDBC/out/production/day01JDBC/jdbc.properties - 注意路径中的
%20, 该字符代表一个空格符, 当FileReader加载这个路径的时候就会出现异常
- 文件路径为:
-
解决方案: 将文件获取的方式改为获取
InputStreamInputStream inputStream = classLoader.getResourceAsStream("jdbc.properties"); properties.load(inputStream); -
或者手动修改读取到的url
String path = JsoupDemo1.class.getClassLoader().getResource("schema/student.xml").getPath(); path = path.replace("%20", " ");