Does BufferedReader.ready() method ensure that readLine() method does not return NULL?

前端 未结 4 1441
既然无缘
既然无缘 2020-11-30 12:20

I have such code to read a text file using BufferedReader:

BufferedReader reader=null;
    try {
        reader = new BufferedReader(new FileRea         


        
4条回答
  •  悲&欢浪女
    2020-11-30 13:07

    Look at the API for ready.

    What you're doing is wrong. ready() only tells you if the stream is readable and valid. Read the comment under return on that link as well.

    What you want to do is:

    String thisLine;
    
    //Loop across the arguments
    for (int i=0; i < args.length; i++) {
    
      //Open the file for reading
      try {
        BufferedReader br = new BufferedReader(new FileReader(args[i]));
        while ((thisLine = br.readLine()) != null) { // while loop begins here
          System.out.println(thisLine);
        } // end while 
      } // end try
      catch (IOException e) {
        System.err.println("Error: " + e);
      }
    } // end for
    

提交回复
热议问题