導航:首頁 > 編程語言 > 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獲取當前類的路徑相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:612
php5314 瀏覽:352
完美國際安裝到哪個文件夾 瀏覽:664
什麼app可以掃一掃做題 瀏覽:535
程序員編碼論壇 瀏覽:921
淘點是什麼app 瀏覽:656
中國高等植物pdf 瀏覽:451
51單片機時間 瀏覽:179
後台如何獲取伺服器ip 瀏覽:262
單片機流水燈程序c語言 瀏覽:230
程序員第二職業掙錢 瀏覽:237
運行里怎麼輸入伺服器路徑 瀏覽:835
pythonstepwise 瀏覽:506
劉一男詞彙速記指南pdf 瀏覽:59
php認證級別 瀏覽:364
方舟編譯啥時候推送 瀏覽:1007
php手機驗證碼生成 瀏覽:672
哲學思維pdf 瀏覽:13
凌達壓縮機有限公司招聘 瀏覽:531
weblogic命令部署 瀏覽:33