Site icon WebArtDeveloper

How to Read from the File using FileInputStream class in Core Java

In this tutorial how to read from the file using FileInputStream class in Core Java is shown.

Code:

FileInputStreamDemo.java

import java.io.*;

class Test4
{
public static void main(String[] args)
{
String fileName=”temp1.txt”;

try
{
byte[] buffer = new byte[1000];
FileInputStream inputStream = new FileInputStream(fileName);
int total = 0;
int nRead = 0;

while((nRead = inputStream.read(buffer))!= -1)
{
System.out.println(new String(buffer));
total += nRead;
}
inputStream.close();
System.out.println(“Read “+total+” bytes”);
}
catch(FileNotFoundException ex)
{
System.out.println(“Unable to open file ‘”+fileName+”‘”);
}
catch(IOException ex)
{
System.out.println(“Error readinf file ‘”+fileName+”‘”);
}
}
}

https://youtu.be/K20PDBdB6Wo

Exit mobile version