File NIO learning experience

Source: Internet
Author: User

The Files class is java. nio. A major entry point of the file package. This class provides a series of static methods to read, write, and manipulate Files or directories. The Files method works on the instance of the Path object.

  • Detect files or directories

Verify whether an exists file or directory can use exists (Path path, LinkOption [] option) and notExists (Path path, LinkOption [] option ). It should be noted that there are three possible results for verifying whether a file exists

  1. File exists
  2. File does not exist
  3. The file status is unknown and cannot be verified.

Therefore, we cannot think that! Exists (Path path) and notExists (Path path) are equal

  • Detects file accessibility

We can use isReadable (Path path), isWritable (Path path), and isExcutable (Path path) to determine whether a file is readable, writable, and executable. For example:

  • Check whether the same file is located in the two paths.

    In a file system, we sometimes need to determine whether the same file is specified in the two paths. We can use the isSameFile (Path path1, Path path2) method to determine. :

  • Delete a file or directory

    When we need to delete a file or directory, the new NIO Files provides two methods to accomplish this purpose.

    1. When delete (Path path) is used, if the deleted file does not exist or the deleted directory contains files, NoSuchFileException or DirectoryNotEmptyException will be thrown. For example:

2. Use deleteIfExists (path) to delete a file or directory. An exception is thrown even if the file exists.

  • Copy a file or directory

    The file class provides copy (Path path1, Path path2, CopyOption [] option) to copy files or directories. Whether to throw an exception when the target file exists depends on the CopyOption enumeration value.

    The enumerated values are defined as follows:

    REPLACE_EXISTING: This option indicates that a copy is executed even if the target file exists. If the Symbolic link itself is copied, the target of the link will not be copied, if the file is copied to a non-empty directory, the FileAlreadyExistsException exception is thrown.

    COPY_ATTRIBUTES: This option indicates copying all the attributes related to the source file to the target file. Whether the target file has the attributes copied from the source file depends on the corresponding system or platform, but the last modification time can always be copied correctly, it is cross-platform.

    NOFOLLOW_LINKS: This option is mainly used for Symbolic link. It copies the link itself and is the target of the link.

    In addition, the file class provides a method for copying files and streams: copy (InputStream in, Path path, CopyOption [] option) This method is used to copy input streams to files, copy (Path path, OutputStream out) This method is used to copy files to the output stream.

  • Move a file or directory

    We can use the Move (Path path1, Path path2, CopyOption [] option) method to Move a file or directory. If the target file already exists, whether to throw an exception is related to the passed CopyOptin mark, you can use this method to move empty directories and directories containing files,

    The directory containing the file is moved and no longer contains the file.

The meaning of the StandardCopyOption parameter is as follows:

REPLACE_EXISTING: This option indicates that if the target file continues to be moved and replaced, if the Symbolic link is moved, the link will be replaced with the original target.

ATOMIC_MOVE: as the name suggests, the operation is either completed correctly or failed, and cannot be partially completed.

Import static java. nio. file. StandardCopyOption .*;

......

Files. move (source, target, REPLACE_EXISTING );

In the next article, we will learn about metadata management (manageing metadata)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.