Writer in Java | Writer Class, Methods

Writer in Java is an abstract class that is used to write characters into a file or output devices.

In other words, it defines streaming character output. Since Writer is an abstract class, we cannot instantiate it.

Java writer class is a superclass for all other writer stream classes that are designed for performing all output operations on files.

Writer stream classes are similar to output stream classes with only one difference. Output stream classes are designed to write bytes whereas, writer stream classes are designed to write characters.

Java Writer class declaration


Writer stream class extends Object class and implements Closeable, Flushable, Appendable, and AutoCloseable interfaces. The general syntax to declare writer stream class in java is as follows:

public abstract class Writer
  extends Object
     implements Appendable, Closeable, Flushable

The inheritance diagram for the Writer class is as follows:

java.lang.Object
         java.io.Writer

The most important concrete subclass of the Writer class is OutputStreamWriter. In addition to OutputStreamWriter class, java.io. package also contains several writer classes that write characters into files. They are as follows:

  • FileWriter
  • BufferedWriter
  • CharArrayWriter
  • FilterWriter
  • PrintWriter
  • StringWriter
  • PipeWriter

Field of Writer class


The writer stream class in Java defines the following field that is protected access modifier.

Field Does this
protected Object lock: It is used to synchronize operations on this stream.

Constructors of Writer class


The writer stream class in Java defines two protected constructors. It does not define any public constructor.

1. protected Writer(): This constructor constructs a new character-stream writer whose critical sections will synchronize on the writer itself.

2. protected Writer(Object lock): This constructor constructs a new character-stream writer whose critical sections will synchronize on the given object.

Methods of Writer class in Java


Writer class defines the following methods in java, all of which are public. All these methods are identical to methods available in the OutputStream class. The most important writer class methods are as follows:

1. Writer append​(char c): This method appends the specified character c to the end of the invoking output stream. It takes only a single character.

2. Writer append​(CharSequence charSeq): This method appends the specified character sequence to the end of the invoking output
stream.

3. Writer append​(CharSequence charSeq, int start, int end): This method appends a subsequence of the specified character sequence to the end of the output stream.

4. abstract void close(): The close() method closes the output stream, flushing it first, and then releases the system resources.

5. abstract void flush(): This method flushes the stream.


6. static Writer nullWriter(): This method returns a new Writer which discards all characters.

7. void write​(char[ ] buffer[ ]): This method writes an array of characters to the output stream.

8. abstract void write​(char[ ] buffer[ ], int off, int len): This method writes a portion of an array of characters, starting from the specified offset to the output stream.

9. void write​(int c): This method writes a single character to the invoking output stream.

10. void write​(String str): This method writes specified string str to the output stream.

11. void write​(String str, int off, int len): This method writes a portion of a string, beginning at the specified offset.

All the methods defined in Java Writer class will throw an IOException on error conditions.

Writer Stream Class Example Program


Let’s take a very simple example program where we will write data into a file named outputfile.txt and display a message on the console.

Program source code 1:

package javaProgram;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class WriterExample {
public static void main(String [] args)
{		
 try 
 { 
   Writer w = new FileWriter("D:\\outputfile.txt");  
   String data = "Every people love their own country in the world.";  
    w.write(data);  
    w.close();  
        
   System.out.println("Successfully written...");  
 } catch (IOException e) 
   {  
     e.printStackTrace();  
   }  
 }
}
Output:
          Successfully written...

outputfile.txt contents:

Every people love their own country in the world.

Difference between Writer and OutputStream in Java


There are the following differences between Writer and java.io.OutputStream that are as follows:

1. Writer stream class is designed to write characters whereas, OutputStream class is designed to write bytes.

2. Writer provides several append() methods for appending characters to the output stream. These methods exist because of implementing java.lang.Appendable interface by Writer class. In the case of OutputStream, these methods do not exist.

3. Writer defines additional write() methods, including write(String str) method for writing a String object’s character to the output stream. In the case of OutputStream class, these methods do not exist.


Hope that this tutorial has elaborated all the important points related to Writer stream class in Java and its method with example program. I hope that you will have understood the basic points of Writer class.
Thanks for reading!!!
Next ⇒ OutputStreamWriter in Java

⇐ Prev Next ⇒