我有一种方法可以以单词列表格式从文本文件中删除单词,但它会不断删除文件中的所有数据而不是特定单词
public static void Option2Method() throws IOException
{
File inputFile = new File("wordlist.txt");
File tempFile = new File("TempWordlist.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = JOptionPane.showInputDialog(null, "Enter a word to remove");
String currentLine;
while((currentLine = reader.readLine()) != null)
{
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine);
}
boolean successful = tempFile.renameTo(inputFile);
}