ClassLoader classLoader2 = asd.class.getClassLoader();
URL res2 = classLoader.getResource("jdbc.properties");
String path2 = res2.getPath();
System.out.println(path2);
下面是输出结果
/D:/java%e5%ad%a6%e4%b9%a0/javaweb/jdbc/out/production/day01_jdbc/jdbc.properties
java%e5%ad%a6%e4%b9%a0/javaweb/jdbc/out/production 这段路径都是错的
路径中包含中文被编码成符合 URL 规范的字符串,重新解码就可以了,输出前添加下面的代码解码:
path2 = URLDecoder.decode(path2, "utf-8");
不是路径错了,是路径中含有中文,被编码了。