Java - Writer class



Introduction

The Java Writer class is a abstract class for writing to character streams.

Class declaration

Following is the declaration for Java.io.Writer class −

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

Field

Following are the fields for Java.io.Writer class −

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No. Constructor & Description
1

protected Writer()

This creates a new character-stream writer whose critical sections will synchronize on the writer itself.

2

protected Writer(Object lock)

This creates a new character-stream writer whose critical sections will synchronize on the given object.

Class methods

Sr.No. Method & Description
1 Writer append(char c)

This method appends the specified character to this writer.

2 Writer append(CharSequence csq)

This method appends the specified character sequence to this writer.

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

This method appends a subsequence of the specified character sequence to this writer.

4 abstract void close()

This method loses the stream, flushing it first.

5 abstract void flush()

This method flushes the stream.

6 void write(char[] cbuf)

This method writes an array of characters.

7 abstract void write(char[] cbuf, int off, int len)

This method writes a portion of an array of characters.

8 void write(int c)

This method writes a single character.

9 void write(String str)

This method writes a string.

10 void write(String str, int off, int len)

This method writes a portion of a string.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Object

Example - Using StringWriter to Append Characters

The following example shows the usage of Writer append(char c) method.

WriterDemo.java

package com.tutorialspoint;

import java.io.StringWriter;

public class WriterDemo {
   public static void main(String[] args) {
      StringWriter writer = new StringWriter();
      writer.append('H');
      writer.append('i');
      writer.append('!');

      System.out.println("Output: " + writer.toString());
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Output: Hi!

Explanation

  • Each append(char) call adds a single character.

  • "H" + "i" + "!" = "Hi!" is stored in the writer.

Example - Using FileWriter with close()

The following example shows the usage of Writer close() method.

WriterDemo.java

package com.tutorialspoint;

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

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new FileWriter("example1.txt");
         writer.write("Hello, this is a test.");
         writer.close(); // Closing the writer to release resources
         System.out.println("File written and writer closed.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

File written and writer closed.

Explanation

  • A FileWriter writes text to a file.

  • After writing, writer.close() is called to−

    • Flush the stream (if needed),

    • Release file locks and system resources,

    • Prevent memory leaks.

  • Writing again after close() will throw an exception.

Example - Using BufferedWriter with flush()

The following example shows the usage of Writer flush() method.

WriterDemo.java

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new BufferedWriter(new FileWriter("flush1.txt"));
         writer.write("First part of the content.");
         writer.flush();  // Forces the buffer to write to file
         System.out.println("Buffer flushed after first write.");

         writer.write(" Second part added.");
         writer.flush();  // Flush again before closing
         System.out.println("Buffer flushed after second write.");

         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Buffer flushed after first write.
Buffer flushed after second write.

Explanation

  • BufferedWriter uses an internal buffer.

  • flush() ensures the content is written immediately to the file, even if the buffer isn't full.

  • Useful when you want to make sure data is written without closing the stream.

Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy