-3
我正在編寫此代碼以從文本文件中刪除一個單詞,似乎無法使掃描儀正常工作。它將與bufferedreader
一起使用,但我不允許使用它。我在這裏做錯了什麼?掃描儀不工作,而不是緩衝讀取器
public static void Option2Method(String dictionary) throws IOException
{
File inputFile = new File(dictionary);
File tempFile = new File("TempDict.txt");
String tempword = JOptionPane.showInputDialog(null, "Enter a word to remove");
String lineToRemove = tempword.toLowerCase();
lineToRemove = lineToRemove.replaceAll(",", "");
lineToRemove = lineToRemove.replaceAll("\\.", "");
lineToRemove = lineToRemove.replaceAll("\\?", "");
lineToRemove = lineToRemove.replaceAll(" ", "");
Scanner reader = new Scanner(new File(inputFile));
FileWriter writer = new FileWriter(tempFile);
String currentLine;
while((currentLine = reader.hasNext()) != null)
{
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine + "\n");
}
reader.close();
writer.close();
inputFile.delete();
tempFile.renameTo(inputFile);
}
這兩個類是在相同System.io *。你的意思是「Im not allowed use that」 – 2013-04-09 09:40:42
它是一個項目的一部分,我們還沒有涵蓋BufferedReader但是 – user2205055 2013-04-09 09:42:31
你可以驗證'reader'是否讀取文件? – John 2013-04-28 03:48:35