Java IO Tutorial - Java PrintWriter.print(String s)








Syntax

PrintWriter.print(String s) has the following syntax.

public void print(String s)

Example

In the following code shows how to use PrintWriter.print(String s) method.

//from   w w w.jav  a  2  s.  co m
import java.io.*;

public class Main {

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

         
         PrintWriter pw = new PrintWriter(System.out);

         // print string
         pw.print(s);

         // change the line
         pw.println();

         // print another string
         pw.print("This is an example.");

         // flush the writer
         pw.flush();

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

The code above generates the following result.