获得Java项目文件的相对路径

在网上看到了这样的一段代码,我想以后对自己会有用,也想和大家分享一下!

 1 /**
2 * 获得项目所在路径<br>
3 * @return String<br>
4 */
5 public static String getPath()
6 {
7 URL url = UtilTools.class.getResource("");
8 File file = new File(url.getFile());
9 String path = file.getParent();
10 while (-1 != path.lastIndexOf("bin") || -1 != path.lastIndexOf(".jar"))
11 {
12 file = new File(path);
13 path = file.getParent();
14 }
15 if (path.startsWith("file"))
16 {
17 path = path.replaceAll("file:", "");
18 }
19 path = path + File.separator+"JmailConfig.xml"
20 return path;
21 }



原文地址:https://www.cnblogs.com/ayan/p/2306796.html