Read a file using InputStream in Java
This post will discuss how to read the contents of a file using an InputStream
in Java.
InputStream
abstract class is the super class of all classes representing an input stream of bytes. There are several ways to read the contents of a file using InputStream
in Java:
1. Using Apache Commons IO
An elegant and concise solution is to use the IOUtils
class from Apache Commons IO library whose toString() method accepts an InputStream
and renders its contents as a string using specified encoding, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import org.apache.commons.io.IOUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("doc.txt"); try (InputStream in = new FileInputStream(file)) { String contents = IOUtils.toString(in, StandardCharsets.UTF_8); System.out.println(contents); } catch (IOException e) { e.printStackTrace(); } } } |
2. BufferedReader’s readLine()
method
Another solution is to use the BufferedReader
. The following code read streams of raw bytes using InputStream
and decodes them into characters using a specified charset using an InputStreamReader, and form a string using a platform-dependent line separator. Here, each invocation of the readLine()
method would read bytes from the file and convert them into characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.io.*; class Main { public static String readFile(File file) throws IOException { StringBuilder sb = new StringBuilder(); InputStream in = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; while ((line = br.readLine()) != null) { sb.append(line + System.lineSeparator()); } return sb.toString(); } public static void main(String[] args) { File file = new File("doc.txt"); String content = null; try { content = readFile(file); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
3. InputStream’s read()
method
InputStream’s read() method reads a byte of data from the input stream. It returns the next byte of data, or -1 if the end of the file is reached, and throws an IOException
if an I/O error occurs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; class Main { public static void main(String[] args) { File file = new File("doc.txt"); try (InputStream in = new FileInputStream(file)) { int content; while ((content = in.read()) != -1) { System.out.print((char)content); } } catch (Exception e) { e.printStackTrace(); } } } |
The InputStream’s read()
method has an overloaded version that can read specified length bytes of data from the input stream into an array of bytes. We can use this method to read the whole file into a byte array at once. The corresponding bytes then can be decoded into characters with the specified charset using the String constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("doc.txt"); Charset charset = StandardCharsets.UTF_8; try (InputStream in = new FileInputStream(file)) { byte[] bytes = new byte[(int) file.length()]; in.read(bytes); String content = new String(bytes, charset); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } } |
In case the file is too large to read in a single read operation, we can open an input stream of bytes using InputStream
and repeatedly read bytes of data from it into a byte array using another overloaded method read(byte[], int, int) until the end of file has been reached. Finally, to get output in the string format, pass the byte array to the String constructor with a charset for decoding. This is demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("doc.txt"); String content = null; try { try (InputStream in = new FileInputStream(file)) { byte[] bytes = new byte[(int)file.length()]; int offset = 0; while (offset < bytes.length) { int result = in.read(bytes, offset, bytes.length - offset); if (result == -1) { break; } offset += result; } content = new String(bytes, StandardCharsets.UTF_8); } } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
That’s all about reading a file using InputStream in Java.
Read More:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)