<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## [文件和目錄路徑](https://lingcoder.gitee.io/onjava8/#/book/17-Files?id=%e6%96%87%e4%bb%b6%e5%92%8c%e7%9b%ae%e5%bd%95%e8%b7%af%e5%be%84) 一個**Path**對象表示一個文件或者目錄的路徑,是一個跨操作系統(OS)和文件系統的抽象,目的是在構造路徑時不必關注底層操作系統,代碼可以在不進行修改的情況下運行在不同的操作系統上。**java.nio.file.Paths**類包含一個重載方法**static get()\*\*,該方法接受一系列 \*\*String**字符串或一個*統一資源標識符*(URI)作為參數,并且進行轉換返回一個**Path**對象: ~~~ // files/PathInfo.java import java.nio.file.*; import java.net.URI; import java.io.File; import java.io.IOException; public class PathInfo { static void show(String id, Object p) { System.out.println(id + ": " + p); } static void info(Path p) { show("toString", p); show("Exists", Files.exists(p)); show("RegularFile", Files.isRegularFile(p)); show("Directory", Files.isDirectory(p)); show("Absolute", p.isAbsolute()); show("FileName", p.getFileName()); show("Parent", p.getParent()); show("Root", p.getRoot()); System.out.println("******************"); } public static void main(String[] args) { System.out.println(System.getProperty("os.name")); info(Paths.get("C:", "path", "to", "nowhere", "NoFile.txt")); Path p = Paths.get("PathInfo.java"); info(p); Path ap = p.toAbsolutePath(); info(ap); info(ap.getParent()); try { info(p.toRealPath()); } catch(IOException e) { System.out.println(e); } URI u = p.toUri(); System.out.println("URI: " + u); Path puri = Paths.get(u); System.out.println(Files.exists(puri)); File f = ap.toFile(); // Don't be fooled } } /* 輸出: Windows 10 toString: C:\path\to\nowhere\NoFile.txt Exists: false RegularFile: false Directory: false Absolute: true FileName: NoFile.txt Parent: C:\path\to\nowhere Root: C:\ ****************** toString: PathInfo.java Exists: true RegularFile: true Directory: false Absolute: false FileName: PathInfo.java Parent: null Root: null ****************** toString: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples\files\PathInfo.java Exists: true RegularFile: true Directory: false Absolute: true FileName: PathInfo.java Parent: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples\files Root: C:\ ****************** toString: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples\files Exists: true RegularFile: false Directory: true Absolute: true FileName: files Parent: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples Root: C:\ ****************** toString: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples\files\PathInfo.java Exists: true RegularFile: true Directory: false Absolute: true FileName: PathInfo.java Parent: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples\files Root: C:\ ****************** URI: file:///C:/Users/Bruce/Documents/GitHub/onjava/ ExtractedExamples/files/PathInfo.java true */ ~~~ 我已經在這一章第一個程序的**main()**方法添加了第一行用于展示操作系統的名稱,因此你可以看到不同操作系統之間存在哪些差異。理想情況下,差別會相對較小,并且使用**/**或者**\\**路徑分隔符進行分隔。你可以看到我運行在Windows 10 上的程序輸出。 當**toString()**方法生成完整形式的路徑,你可以看到**getFileName()**方法總是返回當前文件名。 通過使用**Files**工具類(我們接下來將會更多地使用它),可以測試一個文件是否存在,測試是否是一個"普通"文件還是一個目錄等等。"Nofile.txt"這個示例展示我們描述的文件可能并不在指定的位置;這樣可以允許你創建一個新的路徑。"PathInfo.java"存在于當前目錄中,最初它只是沒有路徑的文件名,但它仍然被檢測為"存在"。一旦我們將其轉換為絕對路徑,我們將會得到一個從"C:"盤(因為我們是在Windows機器下進行測試)開始的完整路徑,現在它也擁有一個父路徑。“真實”路徑的定義在文檔中有點模糊,因為它取決于具體的文件系統。例如,如果文件名不區分大小寫,即使路徑由于大小寫的緣故而不是完全相同,也可能得到肯定的匹配結果。在這樣的平臺上,**toRealPath()**將返回實際情況下的**Path**,并且還會刪除任何冗余元素。 這里你會看到**URI**看起來只能用于描述文件,實際上**URI**可以用于描述更多的東西;通過[維基百科](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier)可以了解更多細節。現在我們成功地將**URI**轉為一個**Path**對象。 最后,你會在**Path**中看到一些有點欺騙的東西,這就是調用**toFile()**方法會生成一個**File**對象。聽起來似乎可以得到一個類似文件的東西(畢竟被稱為**File**),但是這個方法的存在僅僅是為了向后兼容。雖然看上去應該被稱為"路徑",實際上卻應該表示目錄或者文件本身。這是個非常草率并且令人困惑的命名,但是由于**java.nio.file**的存在我們可以安全地忽略它的存在。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看