# Io / Github / Biezhi / Java11 / Files

## Overview

This module appears to be a small Java 11 file I/O example focused on the `java.nio.file.Files` API. It demonstrates the basic lifecycle of working with a text file: write content, read it back, verify the data round-trips correctly, and delete the file when finished. The module exists as a simple, self-contained example for engineers learning the newer `Files` convenience methods introduced in Java 11.

## Key Classes and Interfaces

### [Example](src/main/java/io/github/biezhi/java11/files/Example.java:12)

`Example` is the only class in this module and acts as the executable entry point for the demonstration. Its `main` method shows the most common text-file operations in a minimal form, without any extra abstraction or helper classes.

#### [Example.main(String[] args)](src/main/java/io/github/biezhi/java11/files/Example.java:14)

- **Purpose:** Demonstrates how to write a string to a file, read it back, and clean up afterward.
- **Parameters:** `String[] args` is accepted in the standard Java entry-point shape, but it is not used.
- **Returns:** `void`.
- **Side effects:** Creates `hello.txt` in the current working directory, prints a boolean result to standard output, and then deletes the file.
- **Behavior:**
  1. Sets `text` to the literal string `"Hello biezhi."`.
  2. Writes that value to `hello.txt` with `Files.writeString(Paths.get("hello.txt"), text)`.
  3. Reads the same file back with `Files.readString(Paths.get("hello.txt"))`.
  4. Compares the original and read values and prints the result of `text.equals(readText)`.
  5. Deletes `hello.txt` with `Files.delete(Paths.get("hello.txt"))`.
- **Error handling:** The method declares `throws Exception`, so any I/O failure propagates to the caller instead of being handled locally.

## How It Works

The module uses a linear, imperative flow with no shared state beyond the temporary file it creates.

1. **Prepare content** — the example defines a literal string to use as the file payload.
2. **Write the file** — `Files.writeString(...)` stores the text in a new file named `hello.txt`.
3. **Read the file** — `Files.readString(...)` loads the file contents back into memory.
4. **Verify round-trip correctness** — `text.equals(readText)` checks that the written and read strings match exactly.
5. **Clean up** — `Files.delete(...)` removes the file so the example leaves no residue behind.

### Relationship diagram

```mermaid
flowchart LR
A["Example"] --> B["main(String[] args)"]
B --> C["Files.writeString"]
B --> D["Files.readString"]
B --> E["Files.delete"]
```

## Data Model

There are no domain entities, DTOs, or persistent models in this module. The only data values are:

- a `String` containing the text payload
- a `Path` created implicitly by `Paths.get("hello.txt")`
- the boolean result of the equality check

## Dependencies and Integration

This module depends only on the JDK file APIs used directly in the source file:

- `java.nio.file.Files`
- `java.nio.file.Paths`

It does not reference any other project modules, frameworks, or external libraries. The example is standalone and can be run from the command line or an IDE as a simple Java application.

## Notes for Developers

- The example writes to a relative path, so the file is created in whatever directory the process starts from.
- Because `main` throws `Exception`, the sample is intentionally lightweight and does not model recovery, logging, or user-facing error messages.
- `Files.delete(...)` will fail if the file does not exist or is in use, so this example assumes the write/read steps succeed before cleanup.
- If you extend this example, consider parameterizing the target path so it does not always operate on `hello.txt`.
- The code is a good starting point for comparing Java 11 convenience methods with older stream-based file APIs.
