The Java System setOut() method reassigns the "standard" output stream.
DeclarationFollowing is the declaration for java.lang.System.setOut() method
public static void setOut(PrintStream out)Parameters
out − This is the standard output stream.
Return ValueThis method does not return any value.
ExceptionSecurityException − if a security manager exists and its checkPermission method doesn't allow reassigning of the standard output stream.
Example: Setting output to a fileThe following example shows the usage of Java System setOut() method. In this program, we've created a FileOutputStream object and initialized it with underlying file.txt. Then using setOut() method, we've set the standard output to the file and then message is printed.
package com.tutorialspoint; 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!!!"); } }Output
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!!!Example: Setting Standard Stream as Console
The following example shows the usage of Java System setOut() method. In this program, using setOut() method, we've set the standard output to the console and then message is printed.
package com.tutorialspoint; public class SystemDemo { public static void main(String[] args) throws Exception { System.setOut(System.out); // redirect the output System.out.println("This will get redirected to console"); } }Output
Let us compile and run the above program, this will produce the following result −
This will get redirected to console
java_lang_system.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4