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


Path is an object added in java1.7 nio for locating files in the file system. Path consists of a series of directory and file name elements separated by special delimiters or delimiters that represent the paths of related files in the system.

The Path implementation is immutable and can be used safely in concurrent programming

Path

Files, unlike File, abstracts the Path of a File or directory through the Path object. Path masks the different representation of paths in different operating systems.

createPath

Creating a Path requires using the Paths utility class, which has only two overloaded GET methods for creating a Path. One is created with multiple separated directory names and the other is created using a URI (Uniform Resource Identifier).

The sample

Create childPath

Create a child Path using the resolve method, which can take a string or a parameter of type Path to create a child Path

The sample

Here it is creating a child Path object, not actually creating a file

Gets the current file system

The sample

Get the root Path and parent Path of the file

The sample

Gets the number of levels of this path and the path of the corresponding level

The sample

Check whether the start/end Path is specified

The sample

Into the File

Monitor file

The register method allows you to monitor file changes. The register method accepts a WatchService (obtained from the file system) and multiple StandardWatchEventKinds (file change events), with the last parameter modiFIERS used to change how objects are registered (optional)

The sample

@SneakyThrows
@Test
public void test(a) {

    Path path = Paths.get("E:"."t");

    WatchService watcher = path.getFileSystem().newWatchService();
    // Listen for create and delete events
    WatchKey register = path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE);
    
    // Create a thread to monitor file changes
    new Thread(() -> {
        for(; ;) {for(WatchEvent<? > event : register.pollEvents()) { System.out.println("Document:" + event.context() + " kinds: " + event.kind());
            }
            boolean valid = register.reset();
            if(! valid) { System.out.println("Quit");
                System.exit(0);
            }
        }
    }).start();

    for (int i = 0; i < 10; i++) {
        // Create a file
        Path file = Files.createFile(path.resolve(i + ".txt"));
        System.out.println("Delete:"+ file); Files.delete(file); }}/* file: 0. TXT kinds: ENTRY_CREATE Delete: E:\t\0. TXT file: 0. TXT file: 1. TXT kinds: ENTRY_DELETE file: 2. TXT kinds: ENTRY_CREATE file: E:\t\2. TXT TXT file: ENTRY_DELETE file: 3. TXT kinds: ENTRY_CREATE Delete: E:\t\3. TXT file: 3. TXT kinds: ENTRY_DELETE file: 4. TXT kinds: TXT file: 4. TXT kinds: ENTRY_DELETE file: 5. TXT kinds: TXT file: 5. TXT kinds: ENTRY_DELETE file: 6. TXT kinds: ENTRY_CREATE file: 6. ENTRY_DELETE file: 7.txt kinds: ENTRY_CREATE Delete: E:\t\7.txt file: 7.txt kinds: ENTRY_DELETE file: 8.txt kinds: Delete: E:\t\8.txt file: 8.txt kinds: ENTRY_DELETE file: 9.txt kinds: ENTRY_CREATE delete: E:\t\9.txt file: 9.txt kinds: ENTRY_DELETE disconnects from target VM at address: '127.0.0.1:50677', transport: 'socket' process terminated with exit code 0 */

Copy the code

The Watchservice only monitors a given directory, not subdirectories. If you want to monitor the entire tree directory, you must place a Watchservice on each subdirectory of the entire tree

File search

You can obtain FileSystem from the path instance and obtain the object PathMatcher that matches the path by calling getPathMatcher of the FileSystem. GetPathMatcher supports Glob and Regex matching patterns.

/home/ */* /home/gus/data on UNIX

The sample

Glob begins with a glob expression for matching, **/ for “current directory and all subdirectories,” * for “anything,” and {} for multiple matches