0

嗨,我想用扫描仪替换代码中的 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();
}
4

1 回答 1

0

看看类的定义hasNextLine和类nextLine中的定义Scanner

虽然new Scanner(fileReader)可行,但您宁愿将文件名直接传递给Scanner的构造函数。

于 2013-04-06T11:21:24.590 回答