Reading data from a file using FileInputStream

Reading data from a file using FileInputStream

First, we should attach a file to a FileInputStream as

FileInputStream fin = new¬†FileInputStream(“file.txt”);

we should read data from the FileInputStream as

ch=fin.read();

Then we should attach the monitor to some output stream, so that output stream will send data to the monitor.

System.out.println(ch);

Finally, we read data from the FileInputStream and write it to System.out

Example
import java.io.FileInputStream;
import java.io.IOException;
class test
{
public static void main(String args []) throws IOException
{
FileInputStream fin = new FileInputStream(“file.txt”);
System.out.println(“File content”);
int ch;
while((ch=fin.read())!=-1);
System.out.println(ch);
fin.close();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *