Java.lang.System.setIn() Method


Advertisements


Description

The java.lang.System.setIn() method reassigns the "standard" input stream.

Declaration

Following is the declaration for java.lang.System.setIn() method

public static void setIn(InputStream in)

Parameters

  • in -- This is the new standard input 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 input stream.

Example

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

package com.tutorialspoint;

import java.lang.*;

public class SystemDemo {

   public static void main(String[] args) throws Exception {
    
   // existing file
   System.setIn(new FileInputStream("file.txt"));

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

   // returns the first character
   System.out.println(ret);              
   }
} 

Let us assume we have a text file file.txt which consist of:

This is System class!!!

Compilation will produce the following result:

T

java_lang_system.htm

Advertisements