Java FileInputsStream Class is used for reading a file, like a Text file, Audio, video, etc. The Java.io.FileInputStream class obtains input bytes from a file in the form of a sequence of bytes.
A java.io.InputStream is a parent class of FileInputStream Class. When coming to reading a file, there is a way to read the file. It depends on what kind of file for reading.
- Raw bytes – For image data.
- Characters – For reading streams of characters, use FileReader.
Class declaration
public class FileInputStream extends InputStream
Java FileInputsStream example
Let’s see the example and how to use a FileInputStream. The below image shows the project structure of where is a file located for reading data from it and what content is inside the text file. The text file name is – ” text.txt”
Example: How to Read single character with Java FileInputsStream
For that, you need to import a Java.io.FileInputStream then create an object for FileInputStream with the file name or complete file pathname. Use the read() method for reading text from the file with a file input stream. Using typecasting convert byte to char, otherwise, the output will print a byte.
Note: don’t forget to add a file with some content, otherwise the program will throw an error.
import java.io.FileInputStream; public class FileInputStreamExample { public static void main(String args[]) { try { FileInputStream fin = new FileInputStream("test.txt"); int i = fin.read(); System.out.print((char) i); fin.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
Output: F
Example: Read all Content from File using Java FileInputStream
Same as the above example but that time using While Loop to print all characters from a
import java.io.FileInputStream; public class FileInputStreamExample { public static void main(String args[]) { try { FileInputStream fin = new FileInputStream("test.txt"); int i=0; while((i=fin.read())!=-1){ System.out.print((char)i); } fin.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
Output: FilesInputStream Example
Java FileInputStream class methods
Method | Description |
---|---|
int available() | A return the estimated number of bytes remaining (that can be read) from the input stream. |
int read() | Reads a byte of data from this input stream. |
int read(byte[] b) | R |
int read(byte[] b, int off, int len) | Read up to len bytes of data from the input stream. |
long skip(long x) | Skip over and discards x bytes of data from the input stream. |
FileChannel getChannel() | Return the unique FileChannel object associated with the file input stream. |
FileDescriptor getFD() | Return the FileDescriptor object. |
protected void finalize() | A method used ensures that the close method of this file. In input, the stream is called when there are no more references to it. |
void close() | The closes file input stream and release system resources associated with the stream. |
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java FileInputsStream Class examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.