IO / NOTES</>↗IO02 Jan 2026
In Java, handling ZIP files is primarily done using the java.util.zip package. The key classes you’ll use are ZipOutputStream for creating (zipping) files and ZipInputStream (or ZipFile) for extracting (unzipping) them. Here is a breakdown of how to perform both operations. 1. Zipping Files To zip files, you wrap a FileOutputStream with a ZipOutputStream. For […]
IO / NOTES</>↗IO02 Jan 2026
To use Files.walk to traverse directories, you call the method with a starting Path. It returns a Stream<Path> that lazily populates as you traverse the file tree in a depth-first manner. The most important best practice when using Files.walk is to use it within a try-with-resources block. This ensures that the underlying resources (the directory […]
IO / NOTES</>↗IO01 Jan 2026
To create a temporary file using java.nio.file.Files.createTempFile, you can use one of two main overloaded methods. This is part of the Java NIO.2 API and is generally preferred over the older File.createTempFile because it returns a Path object and allows for better error handling and file attributes. 1. In the Default Temporary Directory If you […]
IO / NOTES</>↗IO01 Jan 2026
To use Files.lines() to process a text file line by line in Java, you should follow the pattern of returning a Stream<String> within a try-with-resources block. This approach is memory-efficient because it reads the file lazily, meaning it doesn’t load the entire file into memory at once. Basic Implementation package org.kodejava.nio; import java.io.IOException; import java.nio.file.Files; […]
IO / NOTES</>↗IO31 Dec 2025
Using Files.newBufferedReader with a try-with-resources block is the best practice for reading files in Java. Since BufferedReader implements AutoCloseable, the try-with-resources statement ensures that the file handle is automatically closed when the block is finished, even if an exception occurs. Here is how you can implement it: Basic Usage This is the simplest way to […]
IO / NOTES</>↗IO31 Dec 2025
Reading large files in Java efficiently is best achieved by using Stream-based APIs that process the file line-by-line or chunk-by-chunk. This prevents loading the entire file into memory (preventing OutOfMemoryError). Here are the most common and efficient ways to do this: 1. Using Files.lines() (Recommended) This is the most modern and idiomatic way in Java. […]