導航:首頁 > 編程語言 > java獲取當前類的路徑

java獲取當前類的路徑

發布時間:2022-10-03 10:03:58

A. java 獲取路徑的幾種方法

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("");//參數為空
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獲取當前工程路徑

B. Java獲取路徑的幾種方式

獲取當前類的絕對路徑;第1種:File directory = new File("");//參數為空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);結果:C:\Documents and Settings\Administrator\workspace\projectName
獲取當前類的所在工程路徑;第2種: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文件的路徑第3種:System.out.println(System.getProperty("user.dir"));結果:C:\Documents and Settings\Administrator\workspace\projectName
獲取當前工程路徑第4種:System.out.println( System.getProperty("java.class.path"));結果:C:\Documents and Settings\Administrator\workspace\projectName\bin獲取當前工程路徑

C. 如何獲得當前Java文件的路徑

1、利用System.getProperty()函數獲取當前路徑:

System.out.println(System.getProperty("user.dir"));//user.dir指定了當前的路徑

2、使用File提供的函數獲取當前路徑:

File directory = new File("");//設定為當前文件夾

try{

System.out.println(directory.getCanonicalPath());//獲取標準的路徑

System.out.println(directory.getAbsolutePath());//獲取絕對路徑

}catch(Exceptin e){}

File.getCanonicalPath()和File.getAbsolutePath()大約只是對於new File(".")和new
File("..")兩種路徑有所區別。

# 對於getCanonicalPath()函數,「."就表示當前的文件夾,而」..「則表示當前文件夾的上一級文件夾

# 對於getAbsolutePath()函數,則不管」.」、「..」,返回當前的路徑加上你在new File()時設定的路徑

# 至於getPath()函數,得到的只是你在new File()時設定的路徑


Java基礎知識教程:

D. java中怎樣獲取當前路徑的絕對路徑

在jsp和class文件中調用的相對路徑不同。在jsp里,根目錄是WebRoot 在class文件中,根目錄是WebRoot/WEB-INF/classes 當然你也可以用System.getProperty("user.dir")獲取工程的絕對路徑。
另:在Jsp,Servlet,Java中詳細獲得路徑的方法!
1.jsp中取得路徑:

以工程名為TEST為例:

(1)得到包含工程名的當前頁面全路徑:request.getRequestURI()
結果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
結果:/TEST
(3)得到當前頁面所在目錄下全名稱:request.getServletPath()
結果:如果頁面在jsp目錄下 /TEST/jsp/test.jsp
(4)得到頁面所在伺服器的全路徑:application.getRealPath("頁面.jsp")
結果:D:\resin\webapps\TEST\test.jsp
(5)得到頁面所在伺服器的絕對路徑:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
結果:D:\resin\webapps\TEST

2.在類中取得路徑:

(1)類的絕對路徑:Class.class.getClass().getResource("/").getPath()
結果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路徑:System.getProperty("user.dir")
結果:D:\TEST

3.在Servlet中取得路徑:

(1)得到工程目錄:request.getSession().getServletContext().getRealPath("") 參數可具體到包名。
結果:E:\Tomcat\webapps\TEST
(2)得到IE地址欄地址:request.getRequestURL()
結果:http://localhost:8080/TEST/test
(3)得到相對地址:request.getRequestURI()
結果:/TEST/test

E. 通過java獲取當前項目路徑

File file = new File("test");
String path = file.getAbsolutePath();
System.out.println(path.substring(0, path.lastIndexOf(File.separator)));
file.delete();

F. 怎麼得到java類當前路徑

File directory = new File(".");
directory.getCanonicalPath();取得當前路徑

既然用access應該是在windows系統下吧,這樣用應該不會有問題
http://www..com/s?wd=52035271515

G. JAVA怎樣獲取當前路徑

java獲取當前路徑的幾種方法
1、利用System.getProperty()函數獲取當前路徑:
System.out.println(System.getProperty("user.dir"));//user.dir指定了當前的路徑

2、使用File提供的函數獲取當前路徑:
File directory = new File("");//設定為當前文件夾
try{
System.out.println(directory.getCanonicalPath());//獲取標準的路徑
System.out.println(directory.getAbsolutePath());//獲取絕對路徑
}catch(Exceptin e){}

File.getCanonicalPath()和File.getAbsolutePath()大約只是對於new File(".")和new File("..")兩種路徑有所區別。

# 對於getCanonicalPath()函數,「."就表示當前的文件夾,而」..「則表示當前文件夾的上一級文件夾
# 對於getAbsolutePath()函數,則不管」.」、「..」,返回當前的路徑加上你在new File()時設定的路徑
# 至於getPath()函數,得到的只是你在new File()時設定的路徑

比如當前的路徑為 C:/test :
File directory = new File("abc");
directory.getCanonicalPath(); //得到的是C:/test/abc
directory.getAbsolutePath(); //得到的是C:/test/abc
direcotry.getPath(); //得到的是abc

File directory = new File(".");
directory.getCanonicalPath(); //得到的是C:/test
directory.getAbsolutePath(); //得到的是C:/test/.
direcotry.getPath(); //得到的是.

File directory = new File("..");
directory.getCanonicalPath(); //得到的是C:/
directory.getAbsolutePath(); //得到的是C:/test/..
direcotry.getPath(); //得到的是..

H. java中類載入路徑和項目根路徑獲取的幾種方式

// 第一種:獲取類載入的根路徑 D:\git\tie\tie\target\classes
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
// 獲取當前類的所在工程路徑; 如果不加「/」 獲取當前類的載入目錄 D:\git\tie\tie\target\classes\my
File f2 = new File(this.getClass().getResource("").getPath());
System.out.println(f2);
// 第二種:獲取項目路徑 D:\git\tie\tie
File directory = new File("");// 參數為空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);
// 第三種: file:/D:/git/tie/tie/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource("");
System.out.println(xmlpath);
// 第四種: D:\git\tie\tie
System.out.println(System.getProperty("user.dir"));/** 結果: C:\Documents and Settings\Administrator\workspace\projectName
* 獲取當前工程路徑*/// 第五種: 獲取所有的類路徑 包括jar包的路徑

I. java獲取當前路徑的幾種方法

1、利用System.getProperty()函數獲取當前路徑:
System.out.println(System.getProperty("user.dir"));//user.dir指定了當前的路徑

2、使用File提供的函數獲取當前路徑:
File directory = new File("");//設定為當前文件夾
try{
System.out.println(directory.getCanonicalPath());//獲取標準的路徑
System.out.println(directory.getAbsolutePath());//獲取絕對路徑
}catch(Exceptin e){}

File.getCanonicalPath()和File.getAbsolutePath()大約只是對於new File(".")和new File("..")兩種路徑有所區別。

# 對於getCanonicalPath()函數,「."就表示當前的文件夾,而」..「則表示當前文件夾的上一級文件夾
# 對於getAbsolutePath()函數,則不管」.」、「..」,返回當前的路徑加上你在new File()時設定的路徑
# 至於getPath()函數,得到的只是你在new File()時設定的路徑

比如當前的路徑為 C:\test :
File directory = new File("abc");
directory.getCanonicalPath(); //得到的是C:\test\abc
directory.getAbsolutePath(); //得到的是C:\test\abc
direcotry.getPath(); //得到的是abc

File directory = new File(".");
directory.getCanonicalPath(); //得到的是C:\test
directory.getAbsolutePath(); //得到的是C:\test\.
direcotry.getPath(); //得到的是.

File directory = new File("..");
directory.getCanonicalPath(); //得到的是C:\
directory.getAbsolutePath(); //得到的是C:\test\..
direcotry.getPath(); //得到的是..

J. java如何獲取類的絕對路徑

1 用servlet獲取

1.1 獲取項目的絕對路徑

request.getSession().getServletContext().getRealPath("")

1.2 獲取瀏覽器地址

request.getRequestURL()

1.3 獲取當前文件的絕對路徑

request.getSession().getServletContext().getRealPath(request.getRequestURI())
2.獲取當前的classpath路徑

String a2=類名.class.getResource("").toString();
String a3=DBConnection.class.getResource("/").toString();
String a4=DBConnection.class.getClassLoader().getResource("").toString();
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
//輸出很好理解

3、獲取文件的絕對路徑
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num).replace('/', '\\')+"項目名\\WebContent\\文件";

閱讀全文

與java獲取當前類的路徑相關的資料

熱點內容
安卓微信如何更換鈴聲 瀏覽:599
程序員談判場在哪裡 瀏覽:413
山東有線伺服器中斷雲主機 瀏覽:472
java截取文件名 瀏覽:463
jquery教程pdf 瀏覽:164
三略pdf 瀏覽:584
spiflash單片機 瀏覽:11
阿里雲的域名怎麼解析到國外伺服器 瀏覽:299
app客戶端開發用什麼伺服器 瀏覽:293
台灣人能備案雲伺服器嗎雲空間 瀏覽:468
程序員小哥哥都喜歡動漫 瀏覽:374
如何用免費亞馬遜雲搭建伺服器 瀏覽:665
php評論功能實現代碼 瀏覽:526
犀牛中移動物件命令 瀏覽:788
程序員上班期間可以戴耳機嗎 瀏覽:257
伺服器啟動卡怎麼使用 瀏覽:796
逛了一天累趴了來一歌解壓句子 瀏覽:347
谷歌app在哪裡掃碼 瀏覽:991
華為手環加密門禁卡怎麼設置 瀏覽:734
pdf轉xlsx 瀏覽:96