Thursday, February 19, 2009

java All Reader classes

FileReader class:
If is the child class of abstract class reader this can be used for reading charater data from a file.
FileReader Constructors:
1.FileReader fr=new FileReader(String fname)
2.FileReader fr=new FileReader (Filename f)
if the file is not found these constructors raise RTE saying FileNotFound Exception
Methods:
1. int read()throws IOException
return the Unicode of the next charater if it is exists else---1.
2.int read(char[] ch)
returns the number of charaters from the file and populated in the specified array.
3.void close()
the flushing is not reqired while reading data .so MISSING
File Reader Examples:
FileReader fr=new FileReader(“laxman.txt”);
System.out.println(“fr.read());
Char[] ch =new char[200];
Fr.read(ch);
For(char c1:ch)
System.out.println(c1);
Fr.close();

BufferedReader:
By using this we can improve the performance.It contain a separate method readLine() to read single line at a time instead of single charater.
So this is the more convenient class for reading charater data from a file
Constructors:
1.BufferedReader br=new BufferedReader(Reader r)
2.BufferedReader br=new BufferedReader(Reader r,int size)
Methods:
1.int read()throws IOException
to read a single character
2.int read(char[] ch)throws IOException
to read an array of character
3.String readLine()
to read nextline a single line.
If there is no next line return null
BufferedReader Example:
FileReader fr=new FileReader(“laxman.txt”);
BufferedReader br=new BufferedReader(fr);
String s=br.readLine();
While(s!=null)
{
System.out.println(s);
S=br.reaadLine();
}
br.close();
}


0 comments:

Post a Comment