Java.io.PrintWriter.print() Method

Advertisements


Description

The java.io.PrintWriter.print() method prints an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Declaration

Following is the declaration for java.io.PrintWriter.print() method

public void print(Object obj)

Parameters

  • obj -- The object to be printed

Return Value

This method does not return a value.

Exception

  • NA

Example

The following example shows the usage of java.io.PrintWriter.print() method.

package com.tutorialspoint;

import java.io.*;
import java.util.Date;

public class PrintWriterDemo {

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

         // create a new writer
         PrintWriter pw = new PrintWriter(System.out);

         // print object
         pw.print(obj1);

         // change the line
         pw.println();

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

         // change the line
         pw.println();

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

         // flush the writer
         pw.flush();

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

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

Object
2
Wed Mar 21 00:00:00 EET 2012



Advertisements

Advertisements