Java - Writer write(string str) method



Description

The Java Writer write(string str) method writes a string.

Declaration

Following is the declaration for java.io.Writer.write(string str) method.

public void write(string str)

Parameters

str − String to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of Writer write(string str) method

The following example shows the usage of Writer write(string str) method.

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      String str = "Hello world!";

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // write a string
         writer.write(str);

         // flush the writer
         writer.flush();

         // change line and write another string
         writer.write("\nThis is an example");

         // flush the stream again
         writer.flush();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Hello world!
This is an example

Example - Using FileWriter with write(String s)

The following example shows the usage of Writer write(string str) 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("write_string1.txt");

         String message = "Hello, Java Writer!";
         writer.write(message);  // Writes the full string to the file

         writer.close();
         System.out.println("String written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Output: Writing to memory using StringWriter.

Explanation

  • A String is written using write(String s) to a file.

  • FileWriter is used to output text to write_string1.txt.

  • After writing, the writer is closed.

Example - Using StringWriter with write(String s)

The following example shows the usage of Writer write(string str) method.

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new StringWriter();

         String data = "Writing to memory using StringWriter.";
         writer.write(data);  // Writes the entire string to memory buffer

         System.out.println("Output: " + writer.toString());
         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Output: Writing to memory using StringWriter.

Explanation

  • A string is written into an internal buffer using StringWriter.

  • The contents are displayed using writer.toString() instead of writing to a file.

java_io_writer.htm
Advertisements