A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/lang/system_setout.htm below:

Java System setOut() Method

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

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