Java.io.Writer.write() Method

Advertisements


Description

The java.io.Writer.write(String str) method writes a string.

Declaration

Following is the declaration for java.io.Writer.write() 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

The following example shows the usage of java.io.Writer.write() method.

package com.tutorialspoint;

import java.io.*;

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();
      }
   }
}

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

Hello world!
This is an example



Advertisements

Advertisements