Java.lang.System.setErr() Method


Advertisements


Description

The java.lang.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

The following example shows the usage of java.lang.System.setErr() method.

package com.tutorialspoint;

import java.lang.*;

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");
   }
} 

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

java_lang_system.htm

Advertisements