Java中怎样获取项目路径,从网上找了几种比如:
File f = new File(this.getClass().getResource("/").getPath());
或
File f = new File(this.getClass().getResource("").getPath());
或
File directory = new File("");//参数为空
String courseFile = directory.getCanonicalPath() ;
这些只能当mian方法调用是才会获取到正确的项目路径,当非main法调用时获取的是eclipse的安装路径,求指教
已经解决反射获取的编译后的文件夹,获取后截取:
如果要获取WebContent目录下的文件绝对路径怎么办?可以用下面的方法
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num).replace('/', '')+"项目名\WebContent\文件";
希望对你有帮助https://blog.csdn.net/qq_34031691/article/details/121495584