什麼是在我的程序中使用多個掃描儀對象的正確方法。例如,我使用掃描儀讀取文件,然後根據文件中找到的內容,我再次使用掃描儀提示用戶輸入。我的代碼摘錄如下如何在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