有絕對(duì)路徑與相對(duì)路徑兩種:
創(chuàng)新互聯(lián)公司主營(yíng)蘇尼特左網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開發(fā),蘇尼特左h5小程序制作搭建,蘇尼特左網(wǎng)站營(yíng)銷推廣歡迎蘇尼特左等地區(qū)企業(yè)咨詢
絕對(duì)路徑?:以引用文件之網(wǎng)頁(yè)所在位置為參考基礎(chǔ),而建立出的目錄路徑。
絕對(duì)路徑:以Web站點(diǎn)根目錄為參考基礎(chǔ)的目錄路徑。
getClass().getResource() 方法獲得相對(duì)路徑( 此方法在jar包中無效。返回的內(nèi)容最后包含/)
例如 項(xiàng)目在/D:/workspace/MainStream/Test
在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/
public?String?getCurrentPath(){??
//取得根目錄路徑??
String?rootPath=getClass().getResource("/").getFile().toString();??
//當(dāng)前目錄路徑??
String?currentPath1=getClass().getResource(".").getFile().toString();??
String?currentPath2=getClass().getResource("").getFile().toString();??
//當(dāng)前目錄的上級(jí)目錄路徑??
String?parentPath=getClass().getResource("../").getFile().toString();??
return?rootPath;?????????
}
參考資料:
這就是相對(duì)路徑
指的是相對(duì)于工程文件的位置而言
在eclipse的結(jié)構(gòu)圖中的位置
在windows的文件夾里的位置
在查看屬性里的絕對(duì)路徑的位置
代碼來找文件路徑
public?class?Test?{
public?static?void?main(String[]?args)?throws?Exception?{
System.out.println("當(dāng)前目錄的路徑\t"+new?File(".").getCanonicalPath());//?"."表示當(dāng)前目錄
File?file?=?new?File("Buffered.txt");
if(!file.exists()){//如果不存在,就新建該文件
file.createNewFile();
}
System.out.println("Buffered.txt的絕對(duì)路徑\t"+file.getCanonicalPath());
System.out.println("Buffered.txt的相對(duì)路徑\t"+file.getPath());
}
}
輸出
當(dāng)前目錄的路徑 D:\space\workspace\Demo
Buffered.txt的絕對(duì)路徑 D:\space\workspace\Demo\Buffered.txt
Buffered.txt的相對(duì)路徑 Buffered.txt
網(wǎng)站名稱:java項(xiàng)目開發(fā)代碼路徑 java項(xiàng)目代碼大全
網(wǎng)頁(yè)網(wǎng)址:http://jinyejixie.com/article24/dosseje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站改版、網(wǎng)站策劃、動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)