IO / CATATAN</>↗IO03 Jan 2026
Using FileChannel from the java.nio.channels package is a powerful way to perform high-performance file operations. It allows for advanced features like memory-mapped files and direct transfer between channels, which are often much faster than traditional stream-based I/O. Here are the most efficient ways to use FileChannel. 1. Fast File Copying with transferTo or transferFrom This […]
IO / CATATAN</>↗IO03 Jan 2026
To use Files.probeContentType(Path path) in Java, you simply pass a Path object to the method. It returns a string representing the MIME type (e.g., image/png, text/plain) or null if the type cannot be determined. Here is a practical example of how to implement it: package org.kodejava.nio; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public […]
IO / CATATAN</>↗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 / CATATAN</>↗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 / CATATAN</>↗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 / CATATAN</>↗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; […]