[Java study notes (118)] Path and Files processing file system

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. Handling the file system

(I. Overview

       The Path and Files classes encapsulate all the functions needed to process the file system on the machine.

(Two) Path

       Path represents a sequence of directory names, followed by a file name, the first part of the path can be the root path, such as "/", the root path is an absolute path, otherwise it is a relative path. The static Path.get method accepts multiple strings, connects them with the path separator of the default file system, and returns a Path object, as shown below:

Path absolute = Paths.get(/home”, “harry”);

       The Path object does not necessarily correspond to an existing file, but is just an abstract name sequence. When creating a file, you must first create a path. To resolve the path, use the resolve() method to return a path. For p.resolve(q), if q is an absolute path, q will be returned; otherwise, the new path will be returned as p followed by q.

(Three) Files

1. Read and write files

       The Files class can quickly read the contents of files, such as

Read all bytes:

byte[] bytes = Files.readAllBytes(path);

Read all text:

var content = Files.readString(path, charset);

       For reading medium-length text files, you can use the Files class, and for large files, use the IO stream and the reader/writer.

2. Create files and directories

Create a new directory:

Files.createDirectory(path);

Create an empty file:

Files.createFile(path);

       Attributes can be specified during creation, such as owner, permissions, and so on.

3. Copy, move, delete files

Copy files:

Files.copy(fromPath, toPath);

Move files:

Files.move(fromPath, toPath);

       Similarly, you can add options, such as overwriting the existing target path StandardCopyOption.REPLACE_EXISTINGand copying all file attributes StandartCopyOption.COPY_ATTRIBUTES.

4. Access items in the catalog

       The static Files.list method returns an Stream<Path>object that can read each item in the directory. The directory is read lazily. This is very efficient. Since reading the directory involves system resources that need to be closed, the try block is used:

try(Stream<Path> entries = Files.list(pathToDirectory)){
    
    }

       The list method does not enter subdirectories. To process subdirectories, use the Files.walk method. For more fine-grained traversal of directories, you should use the Files.newDirectoryStream object to generate a DirectoryStream. Note that this is not a sub-interface of Stream, but an interface dedicated to directory traversal. It is a sub-interface of Iterable, which can be enhanced in the for loop. Use directory flow in as follows:

try(DirectoryStream<Path> entries = Files.newDirectoryStream(str)){
    
    }

       If you want to access all descendant members of a directory, you can call the walkFileTree method and pass in an object of type FileVisitor. The convenience class SimpleFileVisitor implements the FileVisitor interface. This object has the following notifications:

       When accessing files or directories: FileVisitResult visitFile(…);

       Before the directory is processed: FileVisitResult preVisitDirectory(…);

       After the directory is processed: FileVisitResult postVisitDirectory(…);

       The FileVisitResult object refers to the return result of the operation, which is to continue to access the next file: FileVisitResult.CONTINUEetc.

       The walkFileTree method is usually used for delayed operations. For example, when deleting a directory tree, you need to remove all files in the current directory before you can remove the directory.

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/113358950