December 7, 2023

How to read file in Java – FileInputStream

In Java, we use FileInputStream to read bytes from a file, such as an image file or binary file. The reads a byte at a time, and it will return a -1 if it reached the end of the file. The FileInputStream.available() to check the remaining bytes that can be read. The b[]) to read predefined bytes into a byte array; it will significantly increase the read performance. The common practice uses BufferedInputStream to wrap the FileInputStream to provide a buffer cache to increase the read performance. It’s also common to use InputStreamReader to convert FileInputStream to BufferedReader, and read it line by line.

How to read file in Java – FileInputStream Read More

How to read a file in Java

This article focus on a few of the commonly used methods to read a file in Java. 1 – Files.lines, return a Stream (Java 8), 2 – Files.readString, returns a String (Java 11), max file size 2G. 3 – Files.readAllBytes, returns a byte[] (Java 7), max file size 2G. 4 – Files.readAllLines, returns a List (Java 8) . 5 – BufferedReader, a classic old friend (Java 1.1 -> forever) . 6 – Scanner (Java 1.5)

How to read a file in Java Read More