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_setin.htm below:

Java System setIn() Method

Java System setIn() Method Description

The Java 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: Reading from File

The following example shows the usage of Java System setIn() method. In this program, we've set the FileInputStream as Input using setIn() method. Now using System.in.read(), the first char is read from file and is displayed on the console.

package com.tutorialspoint;

import java.io.FileInputStream;

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

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

This is System class!!!

Compilation will produce the following result −

T
Example: Reading from Console

The following example shows the usage of Java System setIn() method. In this program, we've set the System.in as Input using setIn() method. Now using System.in.read(), the first char is read from console and is displayed on the console.

package com.tutorialspoint;

public class SystemDemo {

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

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

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

Compilation will produce the following result −

T
T

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