0
Im在java中編寫一個字典,用戶輸入單詞並按字母順序存儲在.txt文件中。當我自己使用SortWords方法時,它工作正常,但是當我將它與用於向字典添加單詞的Option1Method結合使用時,它的工作量很大。它像它改變文本文件的文件類型或東西?任何幫助將不勝感激文本文件詞典Java
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();
}
你能正確格式化你的代碼嗎? – Abubakkar 2013-03-27 16:50:40
對不起,新來這個。它應該如何格式化? – user2205055 2013-03-27 16:54:56
這應該讓你開始 – 2013-03-27 16:55:58