我正在为一个程序编写代码,该程序从文本文件中获取单词并将它们按字母顺序排列,但我似乎无法让扫描仪工作?
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);
Scanner scan = new Scanner(fileReader);
scan.nextLine;
String inputLine;
List<String> lineList = new ArrayList<String>();
while ((inputLine = scan.nextLine()) != null) {
lineList.add(inputLine);
}
fileReader.close();
Collections.sort(lineList);
FileWriter fileWriter = new FileWriter(outputFile);
PrintWriter out1 = new PrintWriter(fileWriter);
for (String outputLine : lineList) {
out1.println(outputLine);
}
out1.flush();
out1.close();
fileWriter.close();
}