<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 如何創建路徑 – Java NIO > 原文: [https://howtodoinjava.com/java7/nio/how-to-define-path-in-java-nio/](https://howtodoinjava.com/java7/nio/how-to-define-path-in-java-nio/) 眾所周知,Java SE 7 發行版中引入的[**`Path`**](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html "NIO Path")類是`java.nio.file`包的主要入口點之一。 如果您的應用程序使用 NIO,則應了解有關此類中可用特性的更多信息。 我開始[**NIO 教程**](//howtodoinjava.com/java-nio-tutorials/ "Java NIO Tutorials"),并[定義 NIO 2 中的路徑](//howtodoinjava.com/java-nio-tutorials/ "Java NIO Tutorials")。 ![java nio](https://img.kancloud.cn/1b/c3/1bc34606452b877ebc40de91c4793475_147x103.png) 在本教程中,我列出了在 NIO 中創建`Path`的 6 種方法。 **注意**:我正在為以下位置的文件構建路徑-“`C:/Lokesh/Setup/workspace/NIOExamples/src/sample.txt`”。 我已經事先創建了此文件,并將在以下示例中創建此文件的路徑。 ```java Section in this post: Define absolute path Define path relative to file store root Define path relative to current working directory Define path from URI scheme Define path using file system default Using System.getProperty() to build path ``` 讓我們一一看一下以上所有技術的示例代碼: ## 定義絕對路徑 絕對路徑始終包含根元素和查找文件所需的完整目錄列表。 不再需要更多信息來訪問文件或路徑。 我們將使用帶有以下簽名的`getPath()`方法。 ```java /** * Converts a path string, or a sequence of strings that when joined form a path string, * to a Path. If more does not specify any elements then the value of the first parameter * is the path string to convert. If more specifies one or more elements then each non-empty * string, including first, is considered to be a sequence of name elements and is * joined to form a path string. */ public static Path get(String first, String… more); ``` 讓我們看下面的代碼示例。 ```java //Starts with file store root or drive Path absolutePath1 = Paths.get("C:/Lokesh/Setup/workspace/NIOExamples/src", "sample.txt"); Path absolutePath2 = Paths.get("C:/Lokesh/Setup/workspace", "NIOExamples/src", "sample.txt"); Path absolutePath3 = Paths.get("C:/Lokesh", "Setup/workspace", "NIOExamples/src", "sample.txt"); ``` ## 定義相對于文件存儲根的路徑 相對于文件存儲根的路徑以正斜杠(“`/`”)字符開頭。 ```java //How to define path relative to file store root (in windows it is c:/) Path relativePath1 = FileSystems.getDefault().getPath("/Lokesh/Setup/workspace/NIOExamples/src", "sample.txt"); Path relativePath2 = FileSystems.getDefault().getPath("/Lokesh", "Setup/workspace/NIOExamples/src", "sample.txt"); ``` ## 定義相對于當前工作目錄的路徑 要定義相對于當前工作目錄的 NIO 路徑,請不要同時使用文件系統根目錄(在 Windows 中為`c:/`)或斜杠(“`/`”)。 ```java //How to define path relative to current working directory Path relativePath1 = Paths.get("src", "sample.txt"); ``` ## 定義 URI 方案的路徑 并不經常,但是有時我們可能會遇到這樣的情況,我們想將格式為“`file:///src/someFile.txt`”的文件路徑轉換為 NIO 路徑。 讓我們來看看如何做。 ```java //URI uri = URI.create("file:///c:/Lokesh/Setup/workspace/NIOExamples/src/sample.txt"); //OR URI uri = URI.create("file:///Lokesh/Setup/workspace/NIOExamples/src/sample.txt"); String scheme = uri.getScheme(); if (scheme == null) throw new IllegalArgumentException("Missing scheme"); //Check for default provider to avoid loading of installed providers if (scheme.equalsIgnoreCase("file")) { System.out.println(FileSystems.getDefault().provider().getPath(uri).toAbsolutePath().toString()); } //If you do not know scheme then use this code. This code check file scheme as well if available. for (FileSystemProvider provider: FileSystemProvider.installedProviders()) { if (provider.getScheme().equalsIgnoreCase(scheme)) { System.out.println(provider.getPath(uri).toAbsolutePath().toString()); break; } } ``` ## 使用文件系統默認值定義路徑 這是上述示例的另一種變體,其中可以使用`FileSystems.getDefault().getPath()`方法代替使用`Paths.get()`。 絕對路徑和相對路徑的規則與上述相同。 ```java FileSystem fs = FileSystems.getDefault(); Path path1 = fs.getPath("src/sample.txt"); Path path2 = fs.getPath("C:/Lokesh/Setup/workspace/NIOExamples/src", "sample.txt"); ``` ## 使用`System.getProperty()`構建路徑 好吧,這是不可能的,但是很高興知道。 您還可以使用系統特定的`System.getProperty()`來構建特定文件的路徑。 ```java Path path1 = FileSystems.getDefault().getPath(System.getProperty("user.home"), "downloads", "somefile.txt"); ``` 因此,這是創建 NIO 路徑的 6 種方法。 讓我們合并并運行它們以檢查其輸出。 ```java package com.howtodoinjava.nio; import static java.nio.file.FileSystems.getDefault; import java.net.URI; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.spi.FileSystemProvider; public class WorkingWithNIOPath { public static void main(String[] args) { defineAbsolutePath(); defineRelativePathToRoot(); defineRelativePathToWorkingFolder(); definePathFromURI(); UsingFileSystemGetDefault(); UsingSystemProperty(); } //Starts with file store root or drive private static void defineAbsolutePath() { //Starts with file store root or drive Path absolutePath1 = Paths.get("C:/Lokesh/Setup/workspace/NIOExamples/src", "sample.txt"); Path absolutePath2 = Paths.get("C:/Lokesh/Setup/workspace", "NIOExamples/src", "sample.txt"); Path absolutePath3 = Paths.get("C:/Lokesh", "Setup/workspace", "NIOExamples/src", "sample.txt"); System.out.println(absolutePath1.toString()); System.out.println(absolutePath2.toString()); System.out.println(absolutePath3.toString()); } //Starts with a "/" private static void defineRelativePathToRoot() { //How to define path relative to file store root (in windows it is c:/) Path relativePath1 = FileSystems.getDefault().getPath("/Lokesh/Setup/workspace/NIOExamples/src", "sample.txt"); Path relativePath2 = FileSystems.getDefault().getPath("/Lokesh", "Setup/workspace/NIOExamples/src", "sample.txt"); System.out.println(relativePath1.toAbsolutePath().toString()); System.out.println(relativePath2.toAbsolutePath().toString()); } //Starts without a "/" private static void defineRelativePathToWorkingFolder() { //How to define path relative to current working directory Path relativePath1 = Paths.get("src", "sample.txt"); System.out.println(relativePath1.toAbsolutePath().toString()); } private static void definePathFromURI() { //URI uri = URI.create("file:///c:/Lokesh/Setup/workspace/NIOExamples/src/sample.txt"); //OR URI uri = URI.create("file:///Lokesh/Setup/workspace/NIOExamples/src/sample.txt"); String scheme = uri.getScheme(); if (scheme == null) throw new IllegalArgumentException("Missing scheme"); //check for default provider to avoid loading of installed providers if (scheme.equalsIgnoreCase("file")) System.out.println(FileSystems.getDefault().provider().getPath(uri).toAbsolutePath().toString()); // try to find provider for (FileSystemProvider provider: FileSystemProvider.installedProviders()) { if (provider.getScheme().equalsIgnoreCase(scheme)) { System.out.println(provider.getPath(uri).toAbsolutePath().toString()); break; } } } private static void UsingFileSystemGetDefault() { FileSystem fs = getDefault(); Path path1 = fs.getPath("src/sample.txt"); Path path2 = fs.getPath("C:/Lokesh/Setup/workspace/NIOExamples/src", "sample.txt"); System.out.println(path1.toAbsolutePath().toString()); System.out.println(path2.toAbsolutePath().toString()); } private static void UsingSystemProperty() { Path path1 = FileSystems.getDefault().getPath(System.getProperty("user.home"), "downloads", "somefile.txt"); System.out.println(path1.toAbsolutePath().toString()); } } Output in console: ****defineAbsolutePath**** C:LokeshSetupworkspaceNIOExamplessrcsample.txt C:LokeshSetupworkspaceNIOExamplessrcsample.txt C:LokeshSetupworkspaceNIOExamplessrcsample.txt ****defineRelativePathToRoot**** C:LokeshSetupworkspaceNIOExamplessrcsample.txt C:LokeshSetupworkspaceNIOExamplessrcsample.txt ****defineRelativePathToWorkingFolder**** C:LokeshSetupworkspaceNIOExamplessrcsample.txt ****definePathFromURI**** C:LokeshSetupworkspaceNIOExamplessrcsample.txt C:LokeshSetupworkspaceNIOExamplessrcsample.txt ****UsingFileSystemGetDefault**** C:LokeshSetupworkspaceNIOExamplessrcsample.txt C:LokeshSetupworkspaceNIOExamplessrcsample.txt ****UsingSystemProperty**** C:Usershug13902downloadssomefile.txt ``` **祝您學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看