WHAT'S NEW?

Reading the characters from file using FileReader

Java IO have lot of high level and low level of readers and writers.

Files can be read using both the input stream as well as output stream.




Let us try to read the content from a file using FileReader.

FileReader
- A low level Reader for reading the data from the file.


Constructor:


FileReader(File f)

- Open a reader on the specified object.

FileReader(String s)

- Open a reader on the specified path.



Methods


public int read()

- Reads the ascii value of the character till the EOF(End of File)  is reached.

Code to read the data from the file



import java.io.*;



public class ReadUsingFR

{

public static void main(String[] args)

{

try

{

File f=new File("xyz.txt"); // Replace xyz.txt with your file path.

FileReader fr=new FileReader(f);

int data=0;



while((data=fr,read())!=-1)

{

char c=(char)data;

System.out.print(c);

}

fr.close();

}

catch(Exception e)

{

e.printStackTrace();

}



}

}

0 comments:

Post a Comment