什么是在我的程序中使用多个扫描仪对象的正确方法。例如,我使用扫描仪读取文件,然后根据文件中找到的内容,我再次使用扫描仪提示用户输入。我的代码摘录如下如何在System.in上使用多个扫描仪对象?
....
Scanner f = new Scanner (System.in); //get the file name
String fileName = f.next();
Scanner input = new Scanner(new File(fileName));
while (input.hasNext())
{
String currentLine = input.nextLine();
if (some pattern found) {
Scanner getUserInput = new Scanner (System.in);
String userInput = getUserInput.next();
.....
}
}
....
它似乎并不奏效。我需要使用userInput.close()
吗?我究竟做错了什么。 ?
我不明白的是,第一个System.in
只是获取文件名。之后,它为什么会干扰第二个System.in
。 至于input
对象,它从文件读取而不是从System.in
读取。
断开的链接看起来应该是 - > https://www.securecoding.cert.org/confluence/display/java/FIO06-J.+Do+not+create+multiple+buffered+wrappers+on+一个+单+字节+或+字符+流 – iX3 2015-09-20 03:42:58