Buffered Reader Change

混江龙づ霸主 提交于 2019-12-02 16:31:47

问题


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

回答1:


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

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



来源:https://stackoverflow.com/questions/15849929/buffered-reader-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!