您好我想用扫描仪替换这段代码中的BufferedReader?我写了这段代码但后来意识到我们不允许使用bufferedreader。但不知道如何去做,
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) {
out1.println(outputLine);
}
out1.flush();
out1.close();
fileWriter.close();
}
答案 0 :(得分:0)
查看Scanner
课程中hasNextLine
和nextLine
的定义。
虽然new Scanner(fileReader)
有效,但您可以直接将文件名传递给Scanner
的构造函数。