Java.io.PrintStream.close() Method



Description

The java.io.PrintStream.close() method closes the stream. This is done by flushing the stream and then closing the underlying output stream.

Declaration

Following is the declaration for java.io.PrintStream.close() method.

public void close()

Parameters

NA

Return Value

This method does not return a value.

Exception

NA

Example

The following example shows the usage of java.io.PrintStream.close() method.

package com.tutorialspoint;

import java.io.*;

public class PrintStreamDemo {
   public static void main(String[] args) {
      String s = "Hello World.";

      // create printstream object
      PrintStream ps = new PrintStream(System.out);

      // print our string
      ps.println(s);

      // closing stream
      System.out.println("Closing Stream...");

      // close the stream
      ps.close();
   }
}

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

Hello World.
Closing Stream...
java_io_printstream.htm
Advertisements