The Java System setErr() method reassigns the "standard" error output stream.
DeclarationFollowing 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 ValueThis method does not return any value.
ExceptionSecurityException − if a security manager exists and its checkPermission method doesn't allow reassigning of the standard error output stream.
Example: Setting Error Stream as FileThe 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 fileExample: 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
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