PUBLISHED ON: FEBRUARY 26, 2021
Java Writer Class
In this tutorial, we will learn about the Writer
class in Java. This class is an abstract class of java.io
package so we do not use it directly. We can use its subclasses.
Subclasses of Writer
- BufferedWriter
- OutputStreamWriter
- FileWriter
- StringWriter
Constructor
For the Write
class, we can implement two types of constructor variants given below.
Constructor |
Description |
protected Writer() |
It creates a new character-stream writer whose critical sections will synchronize with the writer itself. |
protected Writer(Object lock) |
It creates a new character-stream writer whose critical sections will synchronize on the given object. |
Methods of Writer Class
All the supported methods of the Writer
class are given below.
Method |
Description |
Writer append(char c) |
It appends the specified character to this writer. |
Writer append(CharSequence csq) |
It appends the specified character sequence to this writer |
Writer append(CharSequence csq, int start, int end) |
It appends a subsequence of the specified character sequence to this writer. |
abstract void close() |
It closes the stream, flushing it first. |
abstract void flush() |
It flushes the stream. |
void write(char[] cbuf) |
It writes an array of characters. |
abstract void write(char[] cbuf, int off, int len) |
It writes a portion of an array of characters. |
void write(int c) |
It writes a single character. |
void write(String str) |
It writes a string. |
void write(String str, int off, int len) |
It writes a portion of a string. |
Example 1
In this example, we are creating a Writer
object using FileWriter
class. This write will write the data inside the output.txt
file. To verify that we check the file and match the content.
package studytonight;
import java.io.FileWriter;
import java.io.Writer;
public class StudyTonight
{
public static void main(String args[])
{
try
{
Writer writer = new FileWriter("E:\\studytonight\\output.txt");
writer.write("Hello Studytonight");
writer.close();
System.out.println("Content is written to the file successfully...");
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Content is written to the file successfully...
output.txt
Hello Studytonight
Example 2
In the following example, we are implementing how to append CharSequence to the writer. After appending that CharSequence we can find that an extra string is added in output also.
package studytonight;
import java.io.FileWriter;
import java.io.Writer;
public class StudyTonight
{
public static void main(String args[])
{
try
{
Writer writer = new FileWriter("E:\\studytonight\\output.txt");
CharSequence char_seq = "Hello";
//append method to append CharSequence
writer.append(char_seq);
writer.write("Hello Studytonight");
writer.close();
System.out.println("Content is written to the file successfully...");
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Content is written to the file successfully...
output.txt
HelloHello Studytonight
Conclusion
In this tutorial, we learned about Writer
class. For this class, most subclasses will override some of the methods defined here to provide higher efficiency, functionality, or both.