
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java System setOut() Method
Description
The Java 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: Setting output to a file
The 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