The Java System setIn() method reassigns the "standard" input stream.
DeclarationFollowing 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 ValueThis method does not return any value.
ExceptionSecurityException − if a security manager exists and its checkPermission method doesn't allow reassigning of the standard input stream.
Example: Reading from FileThe 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 −
TExample: 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