很多朋友都想知道java獲取當(dāng)前路徑有哪幾種方法?下面就一起來了解一下吧~
1、利用System.getProperty()函數(shù)獲取當(dāng)前路徑:?
System.out.println(System.getProperty("user.dir"));//user.dir指定了當(dāng)前的路徑
2、使用File提供的函數(shù)獲取當(dāng)前路徑:?
File?directory?=?new?File("");//設(shè)定為當(dāng)前文件夾? try{? ????System.out.println(directory.getCanonicalPath());//獲取標(biāo)準(zhǔn)的路徑? ????System.out.println(directory.getAbsolutePath());//獲取絕對(duì)路徑? }catch(Exceptin?e){}? File.getCanonicalPath()和File.getAbsolutePath()大約只是對(duì)于new?File(".")和new?File("..")兩種路徑有所區(qū)別。? #?對(duì)于getCanonicalPath()函數(shù),“."就表示當(dāng)前的文件夾,而”..“則表示當(dāng)前文件夾的上一級(jí)文件夾? #?對(duì)于getAbsolutePath()函數(shù),則不管”.”、“..”,返回當(dāng)前的路徑加上你在new?File()時(shí)設(shè)定的路徑? #?至于getPath()函數(shù),得到的只是你在new?File()時(shí)設(shè)定的路徑
以上就是小編今天的分享,希望能夠幫到大家。