Reading a file using FileReader

Reading a file using FileReader

FileReader is useful to read data in the form of characters from a ‘text’ file.

Example
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class test
{
public static void main(String args []) throws IOException
{
int ch;
FileReader fr = null;
try
{
fr = new FileReader(“text”);
}
catch(FileNotFoundException fe)
{
System.out.println(“File not found”);
return;
}
while((ch=fr.read())!=-1)
System.out.println((char)ch);
fr.close();
}
}

Leave a Reply

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