開心生活站

位置:首頁 > IT科技 > 

java獲取當前路徑的方法

IT科技2.75W

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

很多朋友都想知道java獲取當前路徑有哪幾種方法?下面就一起來了解一下吧~

1、利用System.getProperty()函數獲取當前路徑: 

System.out.println(System.getProperty("user.dir"));//user.dir指定了當前的路徑

java獲取當前路徑的方法

2、使用File提供的函數獲取當前路徑: 

File directory = new File("");//設定爲當前文件夾 try{     System.out.println(directory.getCanonicalPath());//獲取標準的路徑     System.out.println(directory.getAbsolutePath());//獲取絕對路徑 }catch(Exceptin e){} File.getCanonicalPath()和File.getAbsolutePath()大約只是對於new File(".")和new File("..")兩種路徑有所區別。 # 對於getCanonicalPath()函數,“."就表示當前的文件夾,而”..“則表示當前文件夾的上一級文件夾 # 對於getAbsolutePath()函數,則不管”.”、“..”,返回當前的路徑加上你在new File()時設定的路徑 # 至於getPath()函數,得到的只是你在new File()時設定的路徑

標籤:路徑 獲取 java