新聞中心
java項(xiàng)目路徑文件怎么寫
有絕對路徑與相對路徑兩種:
創(chuàng)新互聯(lián)公司主營蘇尼特左網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā),蘇尼特左h5小程序制作搭建,蘇尼特左網(wǎng)站營銷推廣歡迎蘇尼特左等地區(qū)企業(yè)咨詢
絕對路徑?:以引用文件之網(wǎng)頁所在位置為參考基礎(chǔ),而建立出的目錄路徑。
絕對路徑:以Web站點(diǎn)根目錄為參考基礎(chǔ)的目錄路徑。
通過java獲取當(dāng)前項(xiàng)目路徑
getClass().getResource() 方法獲得相對路徑( 此方法在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)前目錄的上級目錄路徑??
String?parentPath=getClass().getResource("../").getFile().toString();??
return?rootPath;?????????
}
參考資料:
關(guān)于java代碼中文件路徑的問題
這就是相對路徑
指的是相對于工程文件的位置而言
在eclipse的結(jié)構(gòu)圖中的位置
在windows的文件夾里的位置
在查看屬性里的絕對路徑的位置
代碼來找文件路徑
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的絕對路徑\t"+file.getCanonicalPath());
System.out.println("Buffered.txt的相對路徑\t"+file.getPath());
}
}
輸出
當(dāng)前目錄的路徑 D:\space\workspace\Demo
Buffered.txt的絕對路徑 D:\space\workspace\Demo\Buffered.txt
Buffered.txt的相對路徑 Buffered.txt
文章標(biāo)題:java項(xiàng)目開發(fā)代碼路徑 java項(xiàng)目代碼大全
轉(zhuǎn)載來于:http://fisionsoft.com.cn/article/dosseje.html