Java.io.PrintWriter.print( String s) Method



Description

The java.io.PrintWriter.print() method prints a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted 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(String s)

Parameters

s − The string 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.*;

public class PrintWriterDemo {
   public static void main(String[] args) {
      String s = "Hello world.";
      
      try {
         // create a new writer
         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();
      }
   }
}

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

Hello world.
This is an example.
java_io_printwriter.htm
Advertisements