Writer Class in Java

The Writer class in Java is an abstract class defined in the java.io package and superclass for all character output streams. The writer and all its subclasses were introduced in Java 1.1 version. It implements Appendable, Closeable, and Flushable interfaces, they have append() with overloaded forms, close(), and flush() methods respectively.

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

The Writer and all its sub classes are used to write character/text data to the file or console or network.

Java Writer Class Methods

There are different overloaded methods of write() to write the data. Since they are defined for writing the data so, they don’t return any value.

Modifier and TypeMethodDescription
voidwrite(int c)It writes a single character
voidwrite(char cbuf[])It writes an array of characters.
abstract voidwrite(char[] ch, int off, int len)It writes a portion of an array of characters.
voidwrite(String str)It writes a string.
voidwrite(String str, int off, int len)It writes a portion of a string.

Methods to append, flush and close,

Modifier and TypeMethodDescription
Writerappend(char ch)It appends the specified character to this writer.
Writerappend(CharSequence seq)It appends the specified character sequence to this writer.
Writerappend(CharSequence seq, int off, int len)It appends a subsequence of the specified character sequence to this writer.
voidflush()It flushes the stream.
voidclose()It closes the stream, flushing it first

In addition, nullWriter() method was introduced in Java 11 version,

public static Writer nullWriter()It returns a Writer that discards all characters.

In Writer class in Java, except nullWriter() method all other methods throw IOException when an input/output error occurs.

flush() method

When we write data into the stream then data is not saved in the file directly, it is stored only in the output stream memory, and to save those data from that memory to the file/console, the flush() method must be called. The flush() method flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example, a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Up to Java 1.4 version the flush() method was directly available in the Writer class, but in Java 1.5 version Flushable interface was introduced which contains flush() method, and Writer class implements Flushable interface.

package java.io;
import java.io.IOException;
public interface Flushable {
    void flush() throws IOException;
}

close() method

Since it is a connection based object and uses different resources of the computer like file/console. Therefore, after completion of the program, those connections should be closed and all related resources should be released. For this, close() is given and it is defined in the Closable interface. The actual close() method is defined in the AutoCloseable marker interface which was introduced in Java 1.7 version. The closable interface is extended from the AutoClosable interface.

Whenever we call the close() method on Writer and its subclass object then first it calls the flush() method to flush the stream, and then close the stream. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

Subclasses of Java Writer class

The Writer class Hierarchy,

None of these subclasses having 0 parameter constructor because it is a stream-based connection object where one end is Java application. Therefore second end information must be passed as input to the constructor.

The main problem with the FileWriter is, we have to insert a line separator or new line manually, which is varied from system to system and it is a burden to the programmer. Using FileWriter class we can write data only to the file not to the console because it doesn’t contain any constructor which can connect with any Streams. We can solve this problem by using BufferedWriter classes. BufferedWriter class having a newLine() method which writes system-based new line character.

But BufferedWriter class also having some problem, Whenever we want a new line then every time we have to call newLine() method explicitly, which is burden for us.

In both FileWriter and BufferedWriter classes, we can’t write primitive data directly, we must convert them to String. For example:- if we want to write data 100 to the file then we must pass “100” as String. If we pass 100 directly then it will be stored as the character ‘d’. To solve these problems PrintWriter class can be used.

Among all these sub classes PrintWriter class is the most enhanced way to write character/text/string data.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *