Java獲取工程路徑方法詳解
第一種:
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
結果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
獲取當前類的所在工程路徑;
如果不加“/”:
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);
結果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
獲取當前類的絕對路徑;
第二種
File directory = new File("");//參數(shù)為空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);
結果:
C:\Documents and Settings\Administrator\workspace\projectName
獲取當前類的所在工程路徑;
第三種
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 Settings\Administrator\workspace\projectName
獲取當前工程路徑
第五種
System.out.println( System.getProperty("java.class.path"));
結果:
C:\Documents and Settings\Administrator\workspace\projectName\bin
獲取當前工程路徑
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解WebSocket+spring示例demo(已使用sockJs庫)
本篇文章主要介紹了WebSocket spring示例demo(已使用sockJs庫),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
2022版IDEA創(chuàng)建一個maven項目的超詳細圖文教程
IDEA是用于java語言開發(fā)的集成環(huán)境,并且經(jīng)常用于maven、spring、MyBatis等項目的開發(fā),下面這篇文章主要給大家介紹了關于2022版IDEA創(chuàng)建一個maven項目的超詳細圖文教程,需要的朋友可以參考下2023-02-02
SpringBoot利用Redis實現(xiàn)防止訂單重復提交的解決方案
在涉及訂單操作的業(yè)務中,防止訂單重復提交是一個常見需求,用戶可能會因誤操作或網(wǎng)絡延遲而多次點擊提交訂單按鈕,導致訂單重復提交,所以本文給大家介紹了SpringBoot利用Redis實現(xiàn)防止訂單重復提交的解決方案,需要的朋友可以參考下2024-10-10

