IO / CATATAN</>↗IO31 Des 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 / CATATAN</>↗IO31 Des 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. […]
IO / CATATAN</>↗IO30 Des 2025
In Java 11 and later, Path.of() is the preferred way to create Path instances, effectively replacing Paths.get(). Here is how you can use it: 1. Basic Usage (Replacing Paths.get) The syntax is almost identical. It accepts a string or a sequence of strings to join into a path. package org.kodejava.nio; import java.nio.file.Path; public class PathExample […]
IO / CATATAN</>↗IO29 Des 2025
In Java, java.nio.file.Files.mismatch(Path, Path) is a powerful method introduced in Java 12 that allows you to compare the contents of two files efficiently. It returns the position of the first byte where the two files differ, or -1L if they are identical. How to use Files.mismatch Here is a basic example of how to implement […]
IO / CATATAN</>↗IO28 Des 2025
In Java, Files.readString and Files.writeString (introduced in Java 11) are the most straightforward ways to handle small-to-medium-sized text files. They handle the opening, closing, and encoding for you in a single line of code. Here is how you can use them: 1. Reading a File to a String Files.readString(Path) reads the entire content of a […]
CONCURRENCY / CATATAN</>↗Concurrency27 Des 2025
Debugging concurrency issues (like deadlocks, race conditions, and thread starvation) can feel like chasing ghosts because they are often non-deterministic. Here’s a strategy to tackle them effectively using both design patterns and tools available in your environment. 1. Give Your Threads Meaningful Names The default pool-1-thread-1 names are useless in a thread dump. By using […]