Java IO Tutorial - Java PrintWriter.println(Object x)








Syntax

PrintWriter.println(Object x) has the following syntax.

public void println(Object x)

Example

In the following code shows how to use PrintWriter.println(Object x) method.

/*from w w  w. j  ava  2  s .  c om*/
import java.io.*;
import java.util.Date;

public class Main {

   public static void main(String[] args) {
      Object obj1 = "Object";
      Object obj2 = 2;
      Date date = new Date();
      try {

         
         PrintWriter pw = new PrintWriter(System.out);

         // print object
         pw.println(obj1);

         // print another object
         pw.println(obj2);

         // print a date (it is an object)
         pw.print(date);

         // flush the writer
         pw.flush();

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

The code above generates the following result.