Java獲取工程路徑方法詳解
第一種:
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
結(jié)果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
獲取當前類的所在工程路徑;
如果不加“/”:
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);
結(jié)果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
獲取當前類的絕對路徑;
第二種
File directory = new File("");//參數(shù)為空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);
結(jié)果:
C:\Documents and Settings\Administrator\workspace\projectName
獲取當前類的所在工程路徑;
第三種
URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);
結(jié)果:
file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
獲取當前工程src目錄下selected.txt文件的路徑
第四種
System.out.println(System.getProperty("user.dir"));
結(jié)果:
C:\Documents and Settings\Administrator\workspace\projectName
獲取當前工程路徑
第五種
System.out.println( System.getProperty("java.class.path"));
結(jié)果:
C:\Documents and Settings\Administrator\workspace\projectName\bin
獲取當前工程路徑
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring關(guān)于@Configuration配置處理流程
這篇文章主要介紹了Spring關(guān)于@Configuration配置處理流程,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
SpringBoot實現(xiàn)識別圖片中的身份證號與營業(yè)執(zhí)照信息
這篇文章主要為大家詳細介紹了SpringBoot如何實現(xiàn)識別圖片中的身份證號與營業(yè)執(zhí)照信息,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2024-01-01
elasticsearch節(jié)點間通信的基礎(chǔ)transport啟動過程
這篇文章主要為大家介紹了elasticsearch節(jié)點間通信的基礎(chǔ)transport啟動過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
Java實現(xiàn)Elasticsearch查詢當前索引全部數(shù)據(jù)的完整代碼
這篇文章主要介紹了如何在Java中實現(xiàn)查詢Elasticsearch索引中指定條件下的全部數(shù)據(jù),通過設(shè)置滾動查詢參數(shù)(scroll),可以一次性獲取滿足條件的數(shù)據(jù),而不需要限制每頁的查詢條數(shù)大小,這樣可以避免因數(shù)據(jù)量過大而引發(fā)的性能問題,需要的朋友可以參考下2025-02-02
java自定義ClassLoader加載指定的class文件操作
這篇文章主要介紹了java自定義ClassLoader加載指定的class文件操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
SpringBoot詳細講解如何創(chuàng)建及刷新Spring容器bean
前面看spring源碼時可以發(fā)現(xiàn)refresh()方法十分重要。在這個方法中會加載beanDefinition,同時創(chuàng)建bean對象。那么在springboot中有沒有使用這個refresh()方法呢2022-06-06
JSON序列化導致Long類型被搞成Integer的坑及解決
這篇文章主要介紹了JSON序列化導致Long類型被搞成Integer的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

