@Test
public void path() throws IOException {
System.out.println("用户当前工作目录"+System.getProperty("user.dir"));
File directory = new File("");
String path2 = directory.getCanonicalPath();
System.out.println("当前工作目录1:"+path2);
String path3 = directory.getAbsolutePath();
System.out.println("当前工作目录2:"+path3);
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
System.out.println("类加载器返回默认路径:"+path);
String path1 = ResourceUtils.getURL("classpath:").getPath();
System.out.println("ResourceUtils返回默认路径:"+path1);
String resourcePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("resourcePath返回默认路径:"+resourcePath);
ClassPathResource classPathResource = new ClassPathResource("excel/xx.xlsx");
System.out.println("ClassPathResource返回资源路径:"+classPathResource.getURL());
URL resource = this.getClass().getClassLoader().getResource("excel/xx.xlsx");
System.out.println("类加载器返回资源路径:"+resource.getPath());
URL url = ResourceUtil.getResource("excel/xx.xlsx");
System.out.println("ResourceUtil返回资源路径:"+url.getPath());
}
注意,以上是在Junit测试文件中的结果,工作可以精确到所在模块,而普通类里打印是只有主目录没有模块的,如下:文章来源:https://www.toymoban.com/news/detail-615597.html
public static void main(String[] args) throws IOException {
System.out.println("用户当前工作目录"+System.getProperty("user.dir"));
File directory = new File("");
String path2 = directory.getCanonicalPath();
System.out.println("当前工作目录1:"+path2);
String path3 = directory.getAbsolutePath();
System.out.println("当前工作目录2:"+path3);
}
文章来源地址https://www.toymoban.com/news/detail-615597.html
到了这里,关于java读取资源路径的几种方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!