當前位置:生活全書館 >

IT科技

> java獲取當前路徑

java獲取當前路徑

<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 獲取 路徑
  • 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/dianzi/p085w2.html