Java IO Tutorial - Java PrintStream.close()








Syntax

PrintStream.close() has the following syntax.

public void close()

Example

In the following code shows how to use PrintStream.close() method.

/* w ww.  j av  a  2  s.c o  m*/
import java.io.*;

public class Main {

   public static void main(String[] args) {
      String s = "from java2s.com.";

      
      PrintStream ps = new PrintStream(System.out);

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

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

      // close the stream
      ps.close();

   }
}

The code above generates the following result.