Java IO Tutorial - Java PrintWriter.print(Object obj)








Syntax

PrintWriter.print(Object obj) has the following syntax.

public void print(Object obj)

Example

In the following code shows how to use PrintWriter.print(Object obj) method.

// ww  w .j a va 2s  . co  m
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.print(obj1);

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

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

         pw.flush();

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

The code above generates the following result.