Java.lang.System.setOut() Method



Description

The java.lang.System.setOut() method reassigns the "standard" output stream.

Declaration

Following is the declaration for java.lang.System.setOut() method

public static void setOut(PrintStream out)

Parameters

out − This is the standard output stream.

Return Value

This method does not return any value.

Exception

SecurityException − if a security manager exists and its checkPermission method doesn't allow reassigning of the standard output stream.

Example

The following example shows the usage of java.lang.System.setOut() method.

package com.tutorialspoint;

import java.lang.*;
import java.io.*;

public class SystemDemo {

   public static void main(String[] args) throws Exception {
    
      // create file
      FileOutputStream f = new FileOutputStream("file.txt");

      System.setOut(new PrintStream(f));

      // this text will get redirected to file
      System.out.println("This is System class!!!");
   }
} 

Let us assume we have a text file file.txt which gets generated as an output for our example program.The file consist of −

This is System class!!!
java_lang_system.htm
Advertisements