The snippet library.
Small, practical examples. Search by task or narrow things down below.
Copy a file without replacing an existing destination
Java: Files.copy fails if the destination already exists unless replacement is explicitly enabled.
Java✓ Tested
Create and clean up a temporary file
Java: Create a unique file and remove it in finally even when the operation fails.
Java✓ Tested
Delete a known temporary file if present
Java: deleteIfExists returns whether a file was removed and still reports other I/O errors.
Java✓ Tested
Load a small properties configuration
Java: Use a Reader to make the configuration character encoding explicit.
Java✓ Tested
Move a file to a new name
Java: Files.move changes the path and returns the destination Path.
Java✓ Tested
Normalize relative path components
Java: Path.normalize removes redundant dot components without accessing the filesystem.
Java✓ Tested
Process a text file one line at a time
Java: Use a buffered reader to avoid loading a large file into memory.
Java✓ Tested
Read a file length in bytes
Java: Files.size inspects metadata without loading the file into a Java string.
Java✓ Tested
Visit files recursively
Java: Close the stream returned by Files.walk and sort relative paths for stable output.
Java✓ Tested
Write and read binary bytes
Java: Use the byte-oriented Files methods when text encoding should not be involved.
Java✓ Tested