缓冲的读者变更 [英] Buffered Reader Change

查看:63
本文介绍了缓冲的读者变更的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用扫描器替换这段代码中的BufferedReader?我写了这段代码,但后来意识到我们不允许使用bufferedreader。但是Havent一个线索,甚至可以解决该问题。

Hi I want to replace the BufferedReader in the piece of code with scanner?? I wrote this code but then realized that we're not allowed use bufferedreader. But Havent a clue how to even go about,

 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();
}


推荐答案

看看定义为 hasNextLine Scanner 类中的 nextLine

Take a look at the definitions for hasNextLine and nextLine in the Scanner class.

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

Although new Scanner(fileReader) works, you could rather pass the file name directly into the Scanner's constructor.

这篇关于缓冲的读者变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆