In this tutorial, you will learn how to read a file as InputStream in Java, using FileInputStream class and InputStream class of java.io.
Java – Read file as InputStream
To read a file as InputStream in Java,
- Given a file path, create a new instance of FileInputStream using the given file path, and assign it to an InputStream type object.
- Read the input stream byte by byte using a Java While loop.
If filePath is the given file path, then use the following statement to get the InputStream.
</>
Copy
InputStream inputStream = new FileInputStream(filePath)
1. Read the file “data.txt” as InputStream in Java
In the following program, we take a file path "/Users/tutorialkart/data.txt", read the content of this file using InputStream, and print the bytes of file one by one to standard output.
