1. javase中
//获取src路径的一种方式
String srcPath = this.getClass().getClassLoader().getResource("").getPath();//获取src路径的另一种方式
String srcPath2 = InJavaSe.class.getClassLoader().getResource("").getPath();
//获取路径的另一种方式
String srcPath3 = this.getClass().getResource("/").getPath();
//获取当前类路径<包含package路径>
String classPath = this.getClass().getResource("").getPath();
2. javaee中
this.getServletContext().getRealPath("c.txt")
本文介绍了在Java SE和Java EE环境中获取文件和类路径的方法。针对不同的应用场景,提供了多种获取路径的示例代码,并对比了不同方法的特点。
628

被折叠的 条评论
为什么被折叠?



