Home » Java programming language

Java System class setIn() method with example

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

System class setIn() method

  • setIn() method is available in java.lang package.
  • setIn() method is used to assign again the standard input stream.
  • setIn() method is redirected because it does not read the input from the editor.
  • setIn() method read the input from the standard input stream.
  • setIn() method is a static method, it is accessible with the class name too.
  • setIn() method may throw an exception while reading the input from the standard input stream:
    SecurityException: In this exception checkPermission() method does not allow reassigning of the latest standard input stream when security manager exists.

Syntax:

    public static void setIn(InputStream set_in);

Parameter(s):

  • InputStream set_in – represents the latest standard input stream.

Return value:

The return type of this method is void, it does not return anything.

Example:

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

import java.lang.*;
import java.io.*;

public class SetInMethod {
    public static void main(String[] args) throws Exception {

        // Display file in a specific manner
        System.setIn(new FileInputStream("E://Programs//getProperties().doc"));

        // Read the first character in the file
        char read_first_char = (char) System.in.read();

        // Display first character of the File
        System.out.println(read_first_char);
    }
}

Output

E:\Programs>javac SetInMethod.java
E:\Programs>java SetInMethod
J



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.