有時候想獲取放在工程resources 文件下的文件路徑,下面是幾種方法:
1.
String fileName = this.getClass().getClassLoader().getResource("文件名").getPath();//獲取文件路徑
String fileUtl = this.getClass().getResource("文件名").getFile();
示例路徑結果:/E:/idea_work/sofn-qry-web/target/classes/CityJson.js
2.
File directory = new File("");//參數為空
String courseFile = directory.getCanonicalPath()//標准的路徑 ;
String author =directory.getAbsolutePath()//絕對路徑;
示例路徑結果:E:\idea_work\sofn-qry-web
原文:https://blog.csdn.net/oschina_40188932/article/details/78833754