Java IO - Part 4: File IO - 3

Gökhan Kanber
3 min readFeb 21, 2021
Stars

This article is a part of the Java Programming Language article series.

Contents

  • Walking the File Tree
  • Finding Files
  • Watching a Directory for Changes

Walking the File Tree

The FileVisitor Interface

  • Specifies the required behavior at key points in the traversal process
  • A simple implementation: the SimpleFileVisitor class

Key points

  • When a file is visited: visitFile
  • Before a directory is accessed: preVisitDirectory
  • After a directory is accessed: postVisitDirectory
  • When a failure occurs: visitFileFailed

Start the Process

The FileVisitResult values

  • CONTINUE: file walking should continue.
  • TERMINATE: aborts the file walking.
  • SKIP_SUBTREE: when preVisitDirectory returns this value, the specified directory and its subdirectories are skipped.
  • SKIP_SIBLINGS: when preVisitDirectory returns this value, the specified directory is not visited, postVisitDirectory is not invoked, and no further unvisited siblings are visited. If returned from the postVisitDirectory method, no further siblings are visited.

Finding Files

  • Use pattern matching to locate files
  • Pattern matching: pattern with special characters to compare file names
    List all the HTML files: *.html
  • The java.nio.file package provides a PathMatcher in each file system implementation

Watching a Directory for Changes

  • The java.nio.file package provides a file change notification API: Watch Service API

Watch Service Overview

  • Register a directory (or directories) with event types
  • event types: file creation, file deletion, or file modification
  • The registered process has a thread (or a pool of threads) dedicated to watching for events

Process

  1. Create a WatchService watcher for the file system.
  2. For each directory that you want monitored, register it with the watcher, and get a WatchKey instance.
  3. Implement an infinite loop to wait for incoming events. When an event occurs, the key is signaled and placed into the watcher’s queue.
  4. Retrieve the key from the watcher’s queue. You can obtain the file name from the key.
  5. Retrieve each pending event for the key (there might be multiple events) and process as needed.
  6. Reset the key, and resume waiting for events.
  7. Close the service: The watch service exits when either the thread exits or when it is closed (by invoking its closed method).

WatchKeys are thread-safe and can be used with the java.nio.concurrent package.

Creating a Watch Service and Registering for Events

The StandardWatchEventKinds event types

  • ENTRY_CREATE: a directory entry is created.
  • ENTRY_DELETE: a directory entry is deleted.
  • ENTRY_MODIFY: a directory entry is modified.
  • OVERFLOW: indicates that events might have been lost or discarded. Do not register for the OVERFLOW event to receive it.

Processing Events

Get a watch key

  • poll: returns a queued key, if available. Returns immediately with a null value, if unavailable.
  • poll(long, TimeUnit): returns a queued key, if one is available. If a queued key is not immediately available, the program waits until the specified time. The TimeUnit is nanoseconds, milliseconds, or another unit of time.
  • take: returns a queued key. If no queued key is available, this method waits.

Watch key states

  • Ready: indicates that the key is ready to accept events. When first created, a key is in the ready state.
  • Signaled: indicates that one or more events are queued. Once the key has been signaled, it is no longer in the ready state until the reset method is invoked.
  • Invalid: indicates that the key is no longer active.

Invalid state causes

  • The process explicitly cancels the key by using the cancel method.
  • The directory becomes inaccessible.
  • The watch service is closed.

--

--