在java web进行junit测试时,利用类加载器获取路径
String fileName = this.getClass().getClassLoader().getResource("").getPath()+"books.json";
明明路径名都是正确的,却无法获取到文件。
通过网上搜寻,发现一种用于测试的比较好的方法:
String fileName = null;
try {
fileName = new File("").getCanonicalPath()+"\\config\\user.json";
} catch (IOException e) {
e.printStackTrace();
}
但上述只能用于测试,在发布到web后就不起作用了。