Moment For Technology

Java nio Files

Posted on Aug. 26, 2023, 4:26 a.m. by 梁家銘
Category: The back-end Tag: The back-end java

This is the 15th day of my participation in the August Text Challenge.More challenges in August

Files is a new utility class in nio that Java1.7 added specifically for working with Files and directories. With Files and Path, you can easily create, read, modify, and delete Files or directories.

Check whether files/directories exist

The exists and notExists methods are used to check whether the file/directory referred to by Path exists, and the LinkOption parameter indicates whether links are concerned

The sample

  • exists: Returns true if the file/directory exists

  • notExists: Returns true if the file/directory does not exist

Create a file/directory

The methods used to create Files/directories in Files all start with create and return Path. The FileAttribute parameter (file attributes such as read and write permissions) is optional, as shown in the following figure:

The sample

  • createDirectory; Create a new directory

  • createDirectories: Creates a multi-level directory

  • createTempDirectory: Creates a new temporary directory under the specified directory with a name generated based on the prefix

  • createFile: Create a file

  • createTempFile: Creates a temporary file

Delete files or directories

A file can be deleted using the delete and deleteIfExists methods. Delete differs from deleteIfExists in that it returns false if the object to be deleted does not exist

The sample

  • delete: Deletes a file and throws an exception if the file does not exist

  • deleteIfExists: Delete file if it exists, false if it does not

Lists subdirectories/files

To list subdirectories/files, use list and walk. The difference between a list and walk is that a list returns only immediate subdirectories or files, while Walk returns all subdirectories or files

The sample

  • list: returns a direct subdirectory or fileStream

  • walk: returns all subdirectories or filesStream

Find files

You can use the find method to find files. The find method is used to find the files that meet the rules in the specified directory. The BiPredicate parameter is a functional interface that specifies the rules for the files to be found

The sample

Copy files/directories

You can copy files or directories using the copy method. Copy has two overloaded methods for copying InputStream to a target file or data from a file to an OutputStream

The sample

  • Copy files/directories

  • Copy data from InputStream to a target file. An exception will be raised if the parent directory of the target file does not exist

  • Copies the contents of a local file toOutputStream

Move files

The move method is used to move files. Just like the copy method, move can only move files specified in Path. That is, if Path is a directory, subfiles and directories under Path are not moved

The sample

Writes to a file

Writing data to a file can be done using the write method.

If the OpenOption parameter does not exist, the original content is cleared

The sample

  • Write in bytes

  • Write multiple lines of data

  • Write multiple lines of data, specify the character encoding, write mode is append

Reading file contents

The methods for reading data from a file are lines, readAllLines, and readAllBytes. Lines and readAllLines default to the UTF-8 character set.

The sample

  • lines: Reads all data and returns a Stream

  • readAllLines: Reads all data and returns a List

  • readAllBytes: Reads all data and returns a byte array

Convert to input, output streams, channels

Files also provides methods for turning Files referred to by Path into input, output streams, and channels.

The sample

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.