【发布时间】:2015-05-28 03:14:39
【问题描述】:
有没有办法让我能够对 System.in 输入和 FileInputStream 输入使用相同的扫描仪?
这是我在主类中初始化扫描仪的方式:
public class Nimsys {
public static Scanner keyboardIn;
static {
keyboardIn = new Scanner(System.in);
} ...
在 Nimsys 的主类中,我是这样获取输入的:
String inputString1 = keyboardIn.nextLine();
在另一堂课中,我是如何使用 Nimsys 的扫描仪的:
int inputInt1 = Nimsys.keyboardIn.nextInt();
但现在在我的主类 Nimsys 中,我尝试扫描整个文件 - 到目前为止,我使用了另一个扫描仪,您可以在下面的代码中看到。但是,是否可以全部由原始扫描仪完成?
try
{
inputStream = new Scanner(new FileInputStream("file.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File morestuff.txt was not found");
}
String[] reopenPlayers = new String[100];
int i = 0;
while(inputStream.hasNextLine()){
reopenPlayers[i]=inputStream.nextLine();
System.out.println(reopenPlayers[i]);
}
非常感谢!
汤姆
【问题讨论】:
-
使用单一扫描仪有什么好处?
-
@augray 外部编译所以别无选择