java开发必须了解的文件路径问题

1、获取类文件下的绝对路径

youClass.class.getResource("/").toString().replaceAll("file:/", "")

2、获取项目路径

getServletContext().getRealPath("/");

 3、获取类文件路径

this.getClass().getClassLoader().getResource("/").getPath();

4、获取项目路径

request.getContextPath();

5、获取项目路径
System.getProperty("user.dir")

6、获取顶级路径

new File("/").getAbsolutePath()

7、获取当前类路径

ClassLoader.getSystemResource("")

8、获取当前类路径

Thread.currentThread().getContextClassLoader().getResource("")

发布了54 篇原创文章 · 获赞 6 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/shen251515338/article/details/104570127