
- 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 setErr() Method
Description
The Java System setErr() method reassigns the "standard" error output stream.
Declaration
Following is the declaration for java.lang.System.setErr() method
public static void setErr(PrintStream err)
Parameters
err − This is the new standard error 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 error output stream.
Example: Setting Error Stream as File
The following example shows the usage of Java System setErr() method. In this program, we've created a FileOutputStream object and initialized it with underlying file.txt. Then using setErr() method, we've set the error output to the file and then message is printed.
package com.tutorialspoint; import java.io.FileOutputStream; import java.io.PrintStream; public class SystemDemo { public static void main(String[] args) throws Exception { // create a file FileOutputStream f = new FileOutputStream("file.txt"); System.setErr(new PrintStream(f)); // redirect the output System.err.println("This will get redirected to file"); } }
Output
Let us assume we have a text file file.txt which gets generated as an output for our example program.The file content consist of −
This will get redirected to file
Example: Setting Error Stream as Console
The following example shows the usage of Java System setErr() method. In this program, using setErr() method, we've set the error output to the console and then message is printed.
package com.tutorialspoint; public class SystemDemo { public static void main(String[] args) throws Exception { System.setErr(System.out); // redirect the output System.err.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