Thursday, February 19, 2009

java All writer classes

FileWriter:
This is a child class of writer .this class can be used for writing charater data.
FileWriter constructors:
1.FileWriter fw=new FileWriter(Filename)
create a filewriter object to the given file if the file having this name ,doesn’t exists this constructor automatically creates the corresponding file also.But this is not possible in the case of File constructor.
2.FileWriter fw=new FileWriter(File f)
create FileWriter object for the specified File f .if the file is not already present this constructor wilol create automatically the file also.
3.FileWriter fw=new FileWriter(String file name,Boolean append)
If the second argument is true the data append to the existing content .If it is false ,then the old data is over ridden by new data.
4.FileWriter fw =new FileWriter (File f, boolean append) ------>By default flase.
Methods of FileWriter:
1.void write(int ch)throws IOException
the Corresponding character can be written to the file.
2.void write(String s)throws IOException
writes a String to the file
3.void write(char[] ch)throws IOException
writes a charater array data ti the file
4.void flush()
to guarented that the last character of the data should be written to the file
5.void close()
To the close file writer object
6.void write(int unicodevalue)throws IOException
File writer Example:
Import java.io.*;
Class Sample
{
public static void main(String aff[])
{
File f=new File(“file1.txt”);
System.out.println(f.exists());
FileWriter fw =new FileWriter(f);
System.out.println(f.exists());
Fw.write(100);
Fw.write(“laxman software”);
Char[] ch={‘L’,’a’,’x’,’m’,’a’,’n’};
Fw.write(ch);
Fw.close();
Fw.flush();
}
}

  • while inserting the data ,the programmer is responsible to insert separately the line separator(\n).
  • When we are reading the data ,we have to put inside the char array.
  • we should know the size of the array in advance otherwise it will be a probleam .
  • we have to read the data charater by charater which increase the number of IO operations and decreses the performance of the system.
To overcome this ,SUN people has introduced buffered reader and bufferwriter class
BufferedWriter:
we can create a bufferedwriter object over any writer object.
Construtors:
BufferedWriter bf=new BufferedWriter(Writer w);
BufferedWriter bw=new BufferedWriter(Writer w,int size);
Where size is the size of the buffer
Methods:
1.void write(int i)throws IOException
To write one charater
2.void write(String s)throws IOException
to write the given string
3.void write(char[] ch)throws IOException
4.void newLine()
to insert a new line character
5.void flush()
6.void close()
Q.when compare with java.io.BufferedWriter to java.io.FileWriter which capability exists as a method in only one of .
a.closing system
2b.flushing the stream
c.writng to the stream
d.marking the location in the stream
e.writing the line separator to the string (valid)
Buffered Writer Example:
Class Sample
{
public static void main(String args[])
{
FileWriter fw=new FileWriter(“laxman.txt”);
BufferedWriter bw=new BufferedWriter(fw);
Bw.write(100);
Bw.write(“laxman”);
Bw..newLine();
Bw.write(“scjp”);
Bw.newLinw();
Bw.write(“srnagar”);
Bw.flush();
Bw.close();
Fw.close();
}
}

When ever we are closing the BufferedWriter autpmaticaly the stream opened by fileWriter is also closed.


PrintWriter:
The most convenient class for writing any kind of text data .It has enhanced in 1.5 version.
Constructors:
1.PrintWriter p=new PrintWriter(String name)
2.PrintWriter p=new PrintWriter(File name)
3.PrintWriter p=new PrintWriter(Writer w)
Methods:
1.void write(int ch)------> to write character
2.void write(String s)-----> to write String
3.void write(char[] ch)-----to write character
4.void print(int i) -----directory add the int
5.void print(String s)
6.void print(char[] ch)
7.void print(boolean b)
8.void print(char ch)
9.void print(long l)
10.void println(int i) -----directory add the int
11.void println(String s)
12.void println(char[] ch)
13.void println(boolean b)
14.void println(char ch)
15.void println(long l)
PrintWriter Example:
Import java.io.*;
Class Sample
{
public static void main(String add[])throws IOException
{
PrintWriter pw=new PrintWriter(“laxman.txt”);
Pw.write(100);
Pw.print(100);
Pw.print(true);
Pw.print(“laxman”);
Pw.print(“scjp”);
Pw.print(“Hyderabad”);
Pw.flush();
Pw.close();
}
}

0 comments:

Post a Comment