Java.lang.System.setIn() 方法
描述
java.lang.System.setIn() 方法重新分配"标准"输入流。
声明
以下是 java.lang.System.setIn() 方法的声明。
public static void setIn(InputStream in)
参数
in − 这是新的标准输入流。
返回值
此方法不返回任何值。
异常
SecurityException − 如果存在安全管理器并且其 checkPermission 方法不允许重新分配标准输入流。
示例
下面的例子展示了 java.lang.System.setIn() 方法的使用。
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

