Creating a Writer Class
As mentioned earlier, the Writer class is defined under the package java.io. We must import the java.io.Writer package in order to use the Writer class. There are many subclasses of the Writer class, such as BufferedWriter, StringWriter, InputStreamWriter. All of these classes extend the Writer class. Another subclass named FileWriter extends the InputStreamWriter class, which in turn extends the Writer class.
We will use the FileWriter class to create an object by the following piece of code.
Writer output = new FileWriter();
Similarly, we can use the other classes to create objects of the Writer class.
Methods of Writer Class
There are a number of methods defined in the Writer class. Some of the Writer class methods are discussed below in this section.
The write() method
This method has multiple definitions, depending on the type of arguments supplied. In one format, we take a String of data as input, and in another one, we take a char array as input. In both cases, we write the input data to the Writer.
The append() method
This method takes a single character as input and inserts the character into the writer.
The flush() method
This method is used to write all the data present in the Writer to a specified destination.
The close() method
This method is used to close the Writer. It is important to close the Writer, and as a good coding practice, you should always close the Writer.
Constructors of Writer Class
The following are the constructors of the Writer class.
protected Writer()
It is used to create a new character stream. It has a special property of automatically synchronising with the Writer itself.
protected Writer(Object obj)
It is used to create a new character stream. It automatically synchronizes on the Object obj that is supplied as an argument.
Example
The following is a simple program that demonstrates the uses of the Writer class and its associated methods.
Code:
import java.io.*;
public class Main {
public static void main(String[] args){
try{
Writer writer_obj = new PrintWriter(System.out);
// Writing to out stream using write method
writer_obj.write("Welcome to Coding Ninjas\n");
// Flush the writer object
writer_obj.flush();
// Using the append method of Writer class
writer_obj.append('N');
writer_obj.append('i');
writer_obj.append('n');
writer_obj.append('j');
writer_obj.append('a');
writer_obj.append("\n");
// Flush after operation is completed
writer_obj.flush();
// Close the Writer object
writer_obj.close();
}
catch(Exception e){
System.out.println("Exception is: ");
System.out.println(e);
}
}
}
Output:
Welcome to Coding Ninjas
Ninja
Practice by yourself on java online compiler.
Must Read: Java System Out Println and Hashcode Method in Java
Frequently Asked Questions
-
Does the invocation of a single stream flushing affect the other buffers in the chain?
Yes, the invocation of a single stream flushing will flush all other buffers in the chain.
-
What are the fields of the Writer class in Java?
The protected Object lock is the field of the Writer class in Java. It is used for the synchronization of the operations on the stream.
-
What are some methods that are inherited from java.lang.Object?
Some methods that are inherited from java.lang.Object include the clone, finalize, hashCode, toString methods. This list is not exhaustive, and there exist many more methods.
Conclusion
In this article, we have extensively discussed the Writer class in Java and saw a sample program implementing the Writer class. Read the blog, Reader class in Java on the Coding Ninjas Website to learn more about this topic.
We hope that this blog has helped you enhance your knowledge regarding the Writer class in Java. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow. Happy Coding!