With the release of JDK 7, Java has greatly expanded NIO, enhancing support for file handling and file system features to the point that we call them NIO.2. NIO has become an increasingly important part of file processing because of the capabilities it provides.

  • The java.nio.file.Path interface represents a platform-independent platform Path that describes the location of files in a directory structure.
  • Paths provides the get() method to get Path objects:

Path get(String first, String… More) : used to concatenate multiple strings into a path

  • Path Common methods
    • Boolean endsWith(String path) : Checks whether the path path ends

    • Boolean startsWith(String path) : Checks whether the path path starts

    • Boolean isAbsolute() : checks whether the path isAbsolute

    • Path getFileName() : Returns the file name associated with the calling Path object

    • Path getName(int IDX) : returns the Path name of idX at the specified index position

    • Int getNameCount() : Returns the number of elements following the Path root

    • Path getParent() : Returns the Path object containing the entire Path, excluding the file Path specified by the Path object

    • Path getRoot() : Returns the root Path of the calling Path object

    • Path resolve(Path p) : Resolves a relative Path to an absolute Path

    • Path toAbsolutePath() : Calls the Path object as an absolute Path

    • String toString() : Returns a String representation of the calling Path object

      SeekableByteChannel newByteChannel(Path Path, OpenOption... How: Gets the connection to the specified file, and how specifies how to open it. DirectoryStream newDirectoryStream(Path Path) : Open the directory specified by Path InputStream newInputStream(Path Path, OpenOption... OutputStream newOutputStream(Path Path, OpenOption... how) : Obtain an OutputStream object */ @test public void test7() throws IOException{SeekableByteChannel newByteChannel = Files.newByteChannel(Paths.get("1.jpg"), StandardOpenOption.READ); DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(Paths.get("e:/")); for (Path path : newDirectoryStream) { System.out.println(path); }} /* Files Boolean exists(Path Path, LinkOption... Opts) : Check whether the file exists Boolean isDirectory(Path Path, LinkOption... Boolean isExecutable(Path Path) Boolean isHidden(Path Path) : Boolean isReadable(Path Path) : indicates whether the file isReadable. Boolean isWritable(Path Path) : indicates whether the file isReadable. Boolean notExists(Path Path, LinkOption... Public static <A extends BasicFileAttributes> A readAttributes(Path Path,Class<A> type,LinkOption... Options) : Gets properties associated with the file specified by path. */ @Test public void test6() throws IOException{ Path path = Paths.get("e:/nio/hello7.txt");Copy the code

      // System.out.println(Files.exists(path, LinkOption.NOFOLLOW_LINKS));

      BasicFileAttributes readAttributes = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); System.out.println(readAttributes.creationTime()); System.out.println(readAttributes.lastModifiedTime()); DosFileAttributeView fileAttributeView = Files.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); fileAttributeView.setHidden(false); } /* Files common methods: Path copy(Path SRC, Path dest, CopyOption... How) : copy file Path createDirectory(Path Path, FileAttribute<? >... Attr) : Create a directory Path createFile(Path Path, FileAttribute<? >... Void delete(Path Path) : Delete a file. Path Move (Path SRC, Path dest, CopyOption... Long size(Path Path) : Return path specifies the file size */ @test public void test5() throws IOException{path path1 = paths.get ("e:/nio/hello2.txt"); Path path2 = Paths.get("e:/nio/hello7.txt"); System.out.println(Files.size(path2));Copy the code

      // Files.move(path1, path2, StandardCopyOption.ATOMIC_MOVE); }

      @Test
      public void test4() throws IOException{
      	Path dir = Paths.get("e:/nio/nio2");
      Copy the code

      // Files.createDirectory(dir);

      	Path file = Paths.get("e:/nio/nio2/hello3.txt");
      Copy the code

      // Files.createFile(file);

      Files.deleteIfExists(file); } @Test public void test3() throws IOException{ Path path1 = Paths.get("e:/nio/hello.txt"); Path path2 = Paths.get("e:/nio/hello2.txt"); Files.copy(path1, path2, StandardCopyOption.REPLACE_EXISTING); } /* Paths provides the get() method for obtaining Path objects: Path get(String first, String... More) : used to concatenate multiple strings into a path. Boolean endsWith(String Path) : Checks whether the Path ends. Boolean startsWith(String Path) : Boolean isAbsolute() : Checks whether it is an absolute path path getFileName() : Returns the file name associated with the calling path object path getName(int idx) : Int getNameCount() : int getNameCount() Path getParent() : The Path object contains the entire Path, not the file Path specified by the Path object. Path getRoot() : Path resolve(Path p) : resolve relative Path toAbsolutePath toAbsolutePath() : Returns a call to the Path object String toString() as an absolute Path: */ @test public void test2(){Path Path = paths.get ("e:/nio/hello.txt"); System.out.println(path.getParent()); System.out.println(path.getRoot());Copy the code

      // Path newPath = path.resolve(“e:/hello.txt”); // System.out.println(newPath);

      Path path2 = Paths.get("1.jpg"); Path newPath = path2.toAbsolutePath(); System.out.println(newPath); System.out.println(path.toString()); } @Test public void test1(){ Path path = Paths.get("e:/", "nio/hello.txt"); System.out.println(path.endsWith("hello.txt")); System.out.println(path.startsWith("e:/")); System.out.println(path.isAbsolute()); System.out.println(path.getFileName()); for (int i = 0; i < path.getNameCount(); i++) { System.out.println(path.getName(i)); }}Copy the code