我在 java 中编写了一个字典,用户输入单词并按字母顺序存储在 .txt 文件中。当我单独使用 SortWords 方法时,它可以正常工作,但是当我将它与 Option1Method 结合使用时,它将单词添加到字典中,它就不起作用了。它就像它改变了文本文件的文件类型之类的?任何帮助将不胜感激
public static void Option1Method() throws IOException {
FileWriter aFileWriter = new FileWriter("wordlist.txt", true);
PrintWriter out = new PrintWriter(aFileWriter);
String word = JOptionPane.showInputDialog(null, "Enter a word");
out.println(word);
out.close();
aFileWriter.close();
String inputFile = "wordlist.txt";
String outputFile = "wordlist.txt";
FileReader fileReader = new FileReader(inputFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputLine;
List<String> lineList = new ArrayList<String>();
while ((inputLine = bufferedReader.readLine()) != null) {
lineList.add(inputLine);
}
fileReader.close();
Collections.sort(lineList);
FileWriter fileWriter = new FileWriter(outputFile);
PrintWriter out1 = new PrintWriter(fileWriter);
for (String outputLine : lineList) {
out.println(outputLine);
}
out1.flush();
out1.close();
fileWriter.close();
}