Home » Java programming language

Java System class setOut() method with example

System class setOut() method: Here, we are going to learn about the setOut() method of System class with its syntax and example.
Submitted by Preeti Jain, on September 12, 2019

System class setOut() method

  • setOut() method is available in java.lang package.
  • setOut() method is used to assign again the standard output stream or in other words, this method is used to reassign the standard output stream.
  • The setOut() method is redirected because it does not write the output on the editor.
  • The setOut() method does not write the output to the proper JTextArea like setErr() method and it returns null.
  • This is a static method, so this method is accessible with the class name too.
  • setOut() method may throw an exception while writing the output to the standard output stream and the description is given below
    SecurityException: In this exception, the checkPermission() method does not allow reassigning of the latest standard output stream when the security manager exists.


    public static void setOut(PrintStream set_out);


  • set_out – represents the latest standard output stream.

Return value:

The return type of this method is void, it returns nothing


// Java program to demonstrate the example of 
// setOut() method of System Class.

import java.lang.*;

public class SetOutMethod {
    public static void main(String[] args) throws Exception {
        // Creating an instance of File
        File file = new File("E://Programs//getProperties().doc");

        // By using exists() method returns true if file exists
        System.out.println("file existing is  = " + file.exists());

        // Creating an instance of FileOutputStream fos
        FileOutputStream fos = new FileOutputStream(file);
        System.setOut(new PrintStream(fos));
        System.out.println("File Writing Done!!");



E:\Programs>java SetOutMethod
file existing is  = true

Output – if file does not exist

E:\Programs>java SetOutMethod

Exception in thread "main" 
    E:/Programs/getProperties().doc (No such file or directory)
	at java.base/ Method)
	at java.base/
	at java.base/<init>(
	at java.base/<init>(
	at SetOutMethod.main(


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.