Java IO Tutorial - Java Writer.write(String str)








Syntax

Writer.write(String str) has the following syntax.

public void write(String str)  throws IOException

Example

In the following code shows how to use Writer.write(String str) method.

/* w  w w  . ja va2s  .c om*/

import java.io.*;

public class Main {

   public static void main(String[] args) {
      String str = "tutorial from java2s.com!";

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

The code above generates the following result.