Java FileInputStream : Byte Streams
Java program uses byte stream to perform the different input and output operations on the bytes. Java byte stream classes are descendant of InputStream and OutputStream.
Java FileInputStream : Byte Streams
- FileInputStream obtains the input bytes from the file.
- The Java.io.FileInputStream class is used to get input bytes from a file
- FileInputStream is usually used for reading streams of raw bytes such as image data
Program : Java FileInputStream - Byte Streams
package com.examples.basic; import java.io.*; class ReadFile { public static void main(String args[]) { try { FileInputStream fin = new FileInputStream("src/files/input.txt"); int i; while ((i = fin.read()) != -1) System.out.print((char) i + " "); fin.close(); } catch (Exception e) { System.out.println(e); } } }
Output :
J a v a i s O O P L a n g u a g e .
Explanation :
In the above example, we need to create one text file called input.txt whose content is like this -
Java is OOP Language.
Now we need to place this file such that we can have correct relative path -
We must give relative path by taking src as root folder. Otherwise we will get following error -
java.io.FileNotFoundException: input.txt (The system cannot find the file specified)
In the next tutorial we will be learning the FileOutputStream.