Read a file using FileInputStream in Java
This post will discuss how to read the contents of a file using a FileInputStream in Java.
In the previous post, we have discussed how to read the contents of a text file using a FileReader in Java. Since FileReader is meant for reading streams of characters, FileInputStream can be used for reading streams of raw bytes such as image data. There are several ways to read the contents of a file using FileInputStream:
1. FileInputStream’s read() method
FileInputStream’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 |
import java.io.File; import java.io.FileInputStream; class Main { public static void main(String[] args) { File file = new File("doc.txt"); try (FileInputStream fis = new FileInputStream(file)) { int content; while ((content = fis.read()) != -1) { System.out.print((char)content); } } catch (Exception e) { e.printStackTrace(); } } } |
The FileInputStream’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’s constructor, 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 java.io.File; import java.io.FileInputStream; 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 (FileInputStream fis = new FileInputStream(file)) { byte[] bytes = new byte[(int) file.length()]; fis.read(bytes); String fileContent = new String(bytes, charset); System.out.println(fileContent); } catch (Exception e) { e.printStackTrace(); } } } |
In case the file is too large to read in a single read operation, we can use another overloaded version of the FileInputStream’s read() method, which can repeatedly read the specified number of bytes into a buffer until the end of file has been reached. 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 36 37 38 39 40 |
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; class Main { public static String readFile(File file, Charset encoding) throws IOException { try (FileInputStream fis = new FileInputStream(file)) { byte[] bytes = new byte[(int)file.length()]; int offset = 0; while (offset < bytes.length) { int result = fis.read(bytes, offset, bytes.length - offset); if (result == -1) { break; } offset += result; } return new String(bytes, encoding); } } public static void main(String[] args) { File file = new File("doc.txt"); String content = null; try { content = readFile(file, StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
2. BufferedReader’s readLine() method
Another solution is to use the BufferedReader with InputStreamReader. The following code read streams of raw bytes using FileInputStream and decodes them into characters using a specified charset using an InputStreamReader, and construct the 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(); FileInputStream fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 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. Using Apache Commons IO
Another elegant solution is to use the IOUtils class from Apache Commons IO library whose toString() method can accept a FileInputStream and renders its contents as a string using specified charset, 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 |
import org.apache.commons.io.IOUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("doc.txt"); try (FileInputStream fis = new FileInputStream(file)) { String contents = IOUtils.toString(fis, StandardCharsets.UTF_8); System.out.println(contents); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about reading a file using FileInputStream 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 :)