java讀取根目錄文件 普通JAVA文件怎么獲取web項(xiàng)目的根目錄?
普通JAVA文件怎么獲取web項(xiàng)目的根目錄? http://localhost:8080/test/test](3)獲取相對(duì)地址:請(qǐng)求.getRequestURI()java中獲取工程中res目錄路徑
普通JAVA文件怎么獲取web項(xiàng)目的根目錄?
http://localhost:8080/test/test
](3)獲取相對(duì)地址:請(qǐng)求.getRequestURI()
java中獲取工程中res目錄路徑的方法?
獲取當(dāng)前類的項(xiàng)目路徑。如果不添加“/”文件,f=新文件(這個(gè).getClass().getResource(“”).getPath())系統(tǒng)輸出打?。╢) 結(jié)果:C:documents and settings管理員工作區(qū)項(xiàng)目名incom/test獲取當(dāng)前類的絕對(duì)路徑;第二:file directory=new file(“”)//參數(shù)為空字符串coursefile=目錄.getCanonicalPath() 系統(tǒng)輸出打?。╟oursefile)結(jié)果:C:documents and settings administrator workspace ProjectName獲取當(dāng)前類的項(xiàng)目路徑。第三:URL xmlpath=這個(gè).getClass().getClassLoader().getResource()選定.txt") 系統(tǒng)輸出打印(xmlpath)結(jié)果:file/C/documents and settings/administrator/workspace/ProjectName/bin/選定.txt獲取當(dāng)前項(xiàng)目SRC目錄選定.txt文件的第四個(gè)路徑:系統(tǒng)輸出打印( 系統(tǒng).getProperty(" 用戶.dir結(jié)果:C:documents并設(shè)置administrators工作區(qū)項(xiàng)目名稱以獲取當(dāng)前項(xiàng)目路徑系統(tǒng)輸出打印( 系統(tǒng).getProperty(" java.class.path結(jié)果:C:文檔和設(shè)置管理員工作區(qū)中的項(xiàng)目名稱以獲取當(dāng)前項(xiàng)目路徑
實(shí)現(xiàn)思想是獲取類路徑首先,然后從類路徑中切出相應(yīng)的項(xiàng)目根路徑(因?yàn)樗李惡晚?xiàng)目的相對(duì)位置)。可以使用類名class.getResource類(“”)。方法獲取當(dāng)前路徑。示例:“stringpath=XMLS.class.getResource( ""). Getpath()”。說(shuō)明:以上語(yǔ)句是在XML編譯后獲取絕對(duì)路徑(由于java運(yùn)行類文件,無(wú)法獲取java文件路徑),然后根據(jù)實(shí)際情況在任意位置截取路徑。