Java writer api

Following web page demonstrates the Java writer api. Brief but to the point description is given below regarding java abstract writer subclasses, field summary, constructor summary and method summary.

Java writer api


Following web page demonstrates the Java writer api. Brief but to the point description is given below regarding  java abstract writer subclasses,  field summary, constructor summary and method summary.

Abstract Writer subclasses

A subclass inside abstract writer class that creates a buffer for the character output stream for its better processing. The buffer created by the class is a  temporary memory where the characters written into character output stream is buffered. The buffer created has its default size which is large enough for most purposes but this size can be specified.

In order to provide the buffering functionality in the filtered stream. This class is layered with other writer subclasses to make filtered stream required.

Syntax for creating class object :- BufferedWriter br = new BufferedWriter(); 
Syntax for creating a filtered stream:- PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter ( "rose.txt" ))) ;

If no buffer is set for the character out stream, then when ever the print() method is invoked, the characters will directly convert in to bytes and will we instantly written to the file, and that would be very disorganized.

CharArrayWriter :- Class CharArrayWriter extends abstract class writer. Class creates a buffer that can be used as a writer. The buffer created increases its size when any data is written into the character output stream. Class also provides some methods like toCharArray() and toString() methods for
retrieving the data.  
Syntax for creating class object :- CharArrayWriter rose = new CharArrayWriter(0);

There is no need to layer BufferedWriter class object for buffering because class ClassArrayWriter creates a self growing buffer by itself for the efficient processing of underlying output stream.

FilterWriter :-  Class FilterWriter extends abstract writer class. Class generates filtered character streams by filtering  the writer passed inside the constructor of the class object. There are some methods that the class contains for passing all the requests to the underlying output stream.  
Class inherits write method from abstract class. Class also inherits methods from java.lang.Object class and they are as follows: clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait .

Syntax used for filtering :- FileWriter filter = new FilterWriter (writer_object_name);
Note : - Subclasses of filter writer class should override the class methods.

OutputStreamWriter :- Class OutputStreamWriter extends the abstract class writer. This class creates  a connecting channel from a character output stream to a binary stream. The class simply turns a character output stream into a byte output stream.
OutputStreamWriter has a specified charset with which it encodes the data written to it,  in to bytes. 
It has to be noted that the data as characters passed to write method are first get encoded in to bytes by the encoding converter defined in the OutputStreamWriter class and then are get buffered and after that, the buffered value is written in to the underlying output stream. 
The buffer can be set to a definite size with which the data will be buffered. Also one can use the default size of the buffer, which is quite large and fit in most cases.  
Syntax for creating the class object :- OutputStreamWriter rachel = new OutputStreamWriter();
Inside the parentheses you can pass the object of ByteArrayOutputStream class object.

Syntax with examples for using the class:- 
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter rachel = new OutputStreamWriter(out);

Writer rose = new BufferedWriter (new OutputStreamWriter (OutputStream arg, String arg));
Here for OutputStream argument you can be System.out  & for String arguments following codes in double quotes can be passed : 

Codes  = CP437, cp850, CP858, Cp1252 .

PipedWriter :- This class extends abstract class Writer.  Character output streams written by the class are called communications pipes. Data written to piped stream can only be  read or viewed through PipedReader class methods. For reading the data ,  both the  class objects need to be connected first. Connection can be done in two ways, as demonstrated below.
Syntax for creating class Objects :- PipedWriter writer = new PipedWriter();
      PipedReader reader = new PipedReader();
Syntax for connecting the classes :- PipedWriter writer = new PipedWriter(reader);
  writer.connect (reader);
Syntax for reading the data :-;
Method throws IOException if circular buffer is empty. This means no data is written on the stream.

PrintWriter :- Class PrintWriter extends Writer class. Class inherits all the methods of PrintStream and is used to write text output streams. The texts written by class are the formatted representations of other Writer class objects. 'formatted representations' means the data is already taken and buffered. Class takes the Writer objects as argument. Class does not show any IOException, but its constructors can generate this exception. In the class with checkError(), programmer can check for any generated errors.

Syntax for creating class object :- PrintWriter pwd = new PrintWriter();
Syntax for applying :- ByteArrayOutputStream out = new ByteArrayOutputStream(0);
   OutputStreamWriter writer = new OutputStreamWriter(out);  

   PrintWriter print = new PrintWriter(writer);
StringWriter :- StringWriter
is a public class that extends abstract class Writer.  Data passed to the object of class through the class methods are stored in a string buffer class object. Values inside the string buffer can be generated on the console as string. In StringWriter class flushing is automatic done when the standard output methods print & println are invoked to print the values hold by the string buffer object.
Methods of the class can still be invoked, even after the underlying stream is closed.

Syntax for creating class object :- StringWriter str = new StringWriter();
Syntax for using class object:-  StringWriter rose = new StringWriter();
    String news = rose.toString();
   StringBuffer = new StringBuffer(news);
   StringBuffer = new StringBuffer(rose.toString());


FileWriter :- Class FileWriter has public access and inherits OutputStreamWriter class. Class usually used for connecting a character output stream to file. Class with its inherited methods can also write character files. Class can only write streams of characters and not the raw bytes. For writing raw bytes FileOutputStream class is used.

Syntax for creating class object:- FileWriter rd = new FileWriter("rose.txt");
Syntax for using the objec in writing filest:- FileWriter eve = new FileWriter("rose.txt");
Above demonstrated usage of the class object, will work only if a file with name "rose.txt" exist.

Syntax for using the class in connecting character output stream to a file :-
    File fd = new File("rose.txt");
    PrintWriter out = new PrintWriter(new FileWriter(fd)); 
    out.write("newstrack india");
Here class FileWriter is connecting the underlying character output stream of PrintWriter to text file "rose.txt";



Writer Field Summary  

lock :- Object lock is used to synchronize the operations taking place inside the underlying character stream. Here by synchronize, it means applying lock feature on methods and other operations taking place inside the stream. In Locking,  java synchronize keyword is used.
This field object is protected that means it will be available to subclasses and classes in the same package.

By synchronizing the methods few errors  that are stopped before generation are as follows:
Thread Interference :- Generates when more than one thread attempts to access same shared data.
Memory Consistency Errors:- Generates when the views of more than one thread mismatch regarding  certain shared memory.
    Example:- For a int variable 'c'  one thread A generates value 4 and other thread generates value 5. 
Note :- There are still several other errors that are avoided  but demonstrated here.

Writer constructor summary

Class has only two constructor forms. 

protected Writer() :- Constructor is used to create a new character stream. And will  be available to subclasses and classes in the same package. 
protected Writer(Object lock) :- With this constructor form a new character stream is created and also synchronization of the operations in stream is done with lock object.
Use of stream created by this constructor form will be available to subclasses and classes in the same package. 

Writer method summary

Abstract class Writer possess several abstract methods that are not implemented in the abstract class, but in subclasses. All the methods that the  class contains are described below.

: - This method is used append or add character data to the underlying character stream. Method possess several constructor forms as mentioned below: 

append(CharSequence csq) ,  append(CharSequence csq, int start, int end) 

With these constructor forms method can append a portion or complete CharSequence interface value in to the stream. 

This method is used to close the underlying stream. Method first flushes the stream and then close it. Method is abstract and void type. Method throws IOException. This exception comes when an input/output operation is takes place.

Syntax for using the method:- PrintWriter out = new PrintWriter(new FileWriter("rose.txt"));


 Method flushes the stream. Flushing means flowing the data from the stream without closing it. Method is abstract and void type. Method throws IOException. This exception comes when an input/output operation is takes place.

Syntax for using the method:- PrintWriter out = new PrintWriter(new FileWriter("rose.txt"));


Method writes characters in to the stream. This method has several constructor forms as mentioned below.
   write(char[] cbuf) :- With this constructor form method writes an array of characters. 
   write(char[] cbuf, int off, int len) :- With this constructor form method writes a portion or part of an array of characters. 
   write(int c) :- With this only a single character can written.
   write(String str) :-
With this a string value can be written in to the stream.
   write(String str, int off, int len):-
With this a subsequence or portion of  string value is written to the stream.

Method's this constructor form write(char[] cbuf, int off, int len) is abstract and void type and  rest all are just void type.

Share on Google+Share on Google+

Java writer api

Posted on: December 12, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java writer api  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
jaffer ali.j
October 2, 2012

how better ajax then java