導航:首頁 > 編程語言 > java文件引用路徑

java文件引用路徑

發布時間:2023-05-21 08:11:39

A. java中如何得到文件路徑

java文件中獲得路徑
Thread.currentThread().getContextClassLoader().getResource("") //獲得資源文件(.class文件)所在路徑
ClassLoader.getSystemResource("")
Class_Name.class.getClassLoader().getResource("")
Class_Name.class .getResource("/")
Class_Name.class .getResource("") // 獲得當前類所在路徑
System.getProperty("user.dir") // 獲得項目根目錄的絕對路徑
System.getProperty("java.class.path") //得到類路徑和包路徑
列印輸出依次如下:
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/com/xml/imp/
F:\work_litao\uri_test
F:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar

B. java中引用外部文件 引用路徑 ../ 是什麼意思

一些符號的含義:
. (一個點)表示為當老皮前目錄
.. (兩個點)表示為上級目錄侍型差
比如你在 dos 中執行 cd .. 那麼當前所在目錄會變成上級目錄
../ 表示上級租世目錄下的某些文件

C. java文件路徑問題

java有個方式是獲得路徑,就是可以獲得com.softfz.tools,用str代表之
你在這個基礎上public static final ImageIcon ask = new ImageIcon(str."/image/ask.gif");

D. java里文件路徑怎麼寫

File
file
=
new
File("D:\\123.txt");
你這種不用絕對路徑是不行的,
只有一個方法,在web工程啟動servlet中獲取到webroot路徑,在servlet的init中使用String
webRoot
=
getServletContext().getRealPath("/");獲取,然後使用這webRoot變數追加路徑,再new
File(),這樣的話要求就是,你的服務必須要啟動,否則不會init,無法得到工程發布目錄的相對路徑

E. 關於引用自己寫的java的包的路徑問題

javac 帶上 classpath 試下
而且,你這兩個文件為啥不在同樣相對應的目錄?

F. java調用txt文件的路徑問題

把代碼提出來。
一種方式:把SegCn.java中的Dictionary.txt路徑改為絕對路徑,例如:d:/src/Dictionary.txt(or d:\\src\\Dictionary.txt )
另一種方式:相對目錄,SegCn.java和Dictionary.txt放在一個目錄下。

G. java中的類路徑引用表是什麼意思

不能用java作為包的關純鎮悉旅指鍵字. 如果你的類路徑是org\a\test.java的話 要引用test.java的做乎文件如果叫main.java並且在org的文件夾下的話

H. java讀取文件路徑問題

在java中獲得文件的路徑在我們做上傳文件操作時是不可避免的。

web 上運行
1:
this.getClass().getClassLoader().getResource("/").getPath();
this.getClass().getClassLoader().getResource("").getPath(); 得到的是 ClassPath的絕對URI路徑。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
System.getProperty("user.dir");
this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 項目的絕對路徑。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

2:
this.getClass().getResource("/").getPath();
this.getClass().getResource("").getPath(); 得到的是當前類 文件的URI目錄。不包括自己!
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/
this.getClass().getResource(".").getPath(); X 不 能運行

3:
Thread.currentThread().getContextClassLoader().getResource("/").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的絕對URI路徑。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
Thread.currentThread().getContextClassLoader().getResource(".").getPath() 得到的是 項目的絕對路徑。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

在本地運行中
1:
this.getClass().getClassLoader().getResource("").getPath();
this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 ClassPath的絕對URI路徑。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
this.getClass().getClassLoader().getResource(".").getPath(); X 不 能運行
2:
this.getClass().getResource("").getPath();
this.getClass().getResource(".").getPath(); 得到的是當前類 文件的URI目錄。不包括自己!
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/
/D:/myProjects/hp/WebRoot/WEB-INF/classes/ 得到的是 ClassPath的絕對URI路徑。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes

3:
Thread.currentThread().getContextClassLoader().getResource(".").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的絕對URI路徑。。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
Thread.currentThread().getContextClassLoader().getResource("/").getPath() X 不 能運行

最後
在Web應用程序中,我們一般通過ServletContext.getRealPath("/")方法得到Web應用程序的根目錄的絕對路徑。
還有request.getContextPath(); 在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic伺服器,項目內部並沒有文件結構的概念,用這種方式是始終得到null,獲取不到路徑,目前還沒有找到具體的解決方案。

I. 在java中怎麼獲得,本文件的路徑

File類有兩個常用方法可以得到文件路徑一個是:getCanonicalPath(),另一個是:getAbsolutePath(),可以
通過File類的實例調用這兩個方法例如file.getAbsolutePath()其中file是File的實例對象。返猛叢下面是一個具體例子:

publicclassPathTest
{
publicstaticvoidmain(String[]args)
漏櫻{
Filefile=newFile(".\src\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}

getAbsolutePath()和getCanonicalPath()的不同之處在於,getCanonicalPath()得到的是一個規范的
路徑,而getAbsolutePath()是用構造File對象的路徑+當前工作目錄。例如在上面的例子中.(點號)代表當前目錄。
getCanonicalPath()就會把它解析為當前目錄但是getAbsolutePath()會把它解析成為目錄名字(目錄名字是點號)。

下面是上面知灶程序在我電腦上的輸出:

G:xhuojkonw.src
G:xhuojkonwsrc

J. 關於java文件路徑的問題

以/開頭宴源的屬於絕對路徑,唯一標識整個操作系森祥攔統中的某個目錄,例如/此胡var/log/www。

閱讀全文

與java文件引用路徑相關的資料

熱點內容
網站怎麼用本地伺服器 瀏覽:120
javadoc導入android 瀏覽:517
文件掃描成pdf 瀏覽:696
涼山火災救援命令 瀏覽:50
壓縮機三相檢測 瀏覽:861
linux怎麼安裝光碟 瀏覽:799
寬頻伺服器無響應是為什麼 瀏覽:226
壓縮包內看圖 瀏覽:912
安卓手機如何發現app後台調用情況 瀏覽:561
程序員簡歷工作描述 瀏覽:159
伺服器中強制疾跑的指令是什麼 瀏覽:54
日語命令性 瀏覽:261
免費的程序編譯游戲 瀏覽:892
如何安裝屏蔽的APP 瀏覽:198
豬臉識別app如何下載 瀏覽:156
卓嵐串口伺服器如何使用 瀏覽:440
pdf周振甫 瀏覽:759
程序員35歲生日圖片 瀏覽:628
矩形密封圈的壓縮量 瀏覽:455
信息安全中圖像加密技術畢業論文 瀏覽:536