Using BufferedReader.readLine() in a while loop properly

淺唱寂寞╮ 提交于 2019-11-27 01:44:22

You're calling br.readLine() a second time inside the loop.
Therefore, you end up reading two lines each time you go around.

also very comprehensive...

try{
    InputStream fis=new FileInputStream(targetsFile);
    BufferedReader br=new BufferedReader(new InputStreamReader(fis));

    for (String line = br.readLine(); line != null; line = br.readLine()) {
       System.out.println(line);
    }

    br.close();
}
catch(Exception e){
    System.err.println("Error: Target File Cannot Be Read");
}

You can use a structure like the following:

 while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }

In case if you are still stumbling over this question. Nowadays things look nicer with Java 8:

try {
  Files.lines(Paths.get(targetsFile)).forEach(
    s -> {
      System.out.println(s);
      // do more stuff with s
    }
  );
} catch (IOException exc) {
  exc.printStackTrace();
}

Thank you to SLaks and jpm for their help. It was a pretty simple error that I simply did not see.

As SLaks pointed out, br.readLine() was being called twice each loop which made the program only get half of the values. Here is the fixed code:

try{
        InputStream fis=new FileInputStream(targetsFile);
        BufferedReader br=new BufferedReader(new InputStreamReader(fis));
        String words[]=new String[5];
        String line=null;
        while((line=br.readLine())!=null){
            words=line.split(" ");
            int targetX=Integer.parseInt(words[0]);
            int targetY=Integer.parseInt(words[1]);
            int targetW=Integer.parseInt(words[2]);
            int targetH=Integer.parseInt(words[3]);
            int targetHits=Integer.parseInt(words[4]);
            Target a=new Target(targetX, targetY, targetW, targetH, targetHits);
            targets.add(a);
        }
        br.close();
    }
    catch(Exception e){
        System.err.println("Error: Target File Cannot Be Read");
    }

Thanks again! You guys are great!

Concept Solution:br.read() returns particular character's int value so loop 
continue's until we won't get -1 as int value and Hence up to there it print 
br.eadline() which returns a line into String form.
Way 1:
while(br.read()!=-1)
 {
  //continues loop until we won't get int value as a -1
  System.out.println(br.readLine());
 }
 Way 2:
 while((line=br.readLine())!=null)
 {
  System.out.println(line);
 }
 Way 3:
 for(String line=br.readLine();line!=null;line=br.readLine())
 {
  System.out.println(line);
 }``
 Way 4:
 It's an advance way to read file using collection and arrays concept 
 How we iterate using for each loop.

check it here http://www.java67.com/2016/01/how-to-use-foreach-method-in-java-8-examples.html

In addition to the answer given by @ramin, if you already have BufferedReader or InputStream, it's possible to iterate through lines like this:

reader.lines().forEach(line -> {
    //...
});

or if you need to process it with given order:

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