Java獲取工程路徑方法詳解
第一種:
File f = new File(this.getClass().getResource('/').getPath()); System.out.println(f);
結果:
C:Documents%20and%20SettingsAdministratorworkspaceprojectNamebin 獲取當前類的所在工程路徑;
如果不加“/”:
File f = new File(this.getClass().getResource('').getPath()); System.out.println(f);
結果:
C:Documents%20and%20SettingsAdministratorworkspaceprojectNamebincomtest 獲取當前類的絕對路徑;
第二種
File directory = new File('');//參數為空 String courseFile = directory.getCanonicalPath() ; System.out.println(courseFile);
結果:
C:Documents and SettingsAdministratorworkspaceprojectName 獲取當前類的所在工程路徑;
第三種
URL xmlpath = this.getClass().getClassLoader().getResource('selected.txt'); System.out.println(xmlpath);
結果:
file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt 獲取當前工程src目錄下selected.txt文件的路徑
第四種
System.out.println(System.getProperty('user.dir'));
結果:
C:Documents and SettingsAdministratorworkspaceprojectName
獲取當前工程路徑
第五種
System.out.println( System.getProperty('java.class.path'));
結果:
C:Documents and SettingsAdministratorworkspaceprojectNamebin 獲取當前工程路徑
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. java實現圖形化界面計算器2. IntelliJ Idea2017如何修改緩存文件的路徑3. IntelliJ IDEA設置條件斷點的方法步驟4. IIS Express 取代 ASP.NET Development Server的配置方法5. python flask框架快速入門6. Spring-Richclient 0.1.0 發布7. javascript設計模式 ? 建造者模式原理與應用實例分析8. 淺談SpringMVC jsp前臺獲取參數的方式 EL表達式9. Python使用oslo.vmware管理ESXI虛擬機的示例參考10. Express 框架中使用 EJS 模板引擎并結合 silly-datetime 庫進行日期格式化的實現方法
