System.setOut() in Java

System.setOut(): This method is available in java.lang.System class of Java.

Syntax:

void java.lang.System.setOut(PrintStream out)

This method reassigns the "standard" output stream.

Parameters: One parameter is required for this method.

out the new standard output stream.

Throws:

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

Approach

Java

import java.io.FileOutputStream;
import java.io.PrintStream;

public class SystemsetOut {
    public static void main(String[] argsthrows Exception {

        System.out.println("Loading file..");
        FileOutputStream fileOut = new FileOutputStream("D:\\hello.txtt");

        PrintStream out = new PrintStream(fileOut);
        System.setOut(out);

        out.close();
        fileOut.close();
    }
}

Output:

Loading file..


No comments:

Post a Comment