138. Reading/writing text files efficiently

In Java, reading files efficiently is a matter of choosing the right approach. For a better understanding of the following example, let's assume that our platform's default charset is UTF-8. Programmatically, the platform's default charset can be obtained via Charset.defaultCharset().

First, we need to distinguish between raw binary data and text files from a Java perspective. Dealing with raw binary data is the job of two abstract classes, that is, InputStream and OutputStream. For streaming files of raw binary data, we focus on the FileInputStream and FileOutputStream classes, which read/write a byte (8 bits) at a time. For famous types of binary data, we also have dedicated classes (for example, ...

Get Java Coding Problems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.