while EOF in JAVA?

后端 未结 7 1408
难免孤独
难免孤独 2020-12-17 06:21

Following to some forums answers I\'m using this:

    while (fileReader.nextLine() != null) {
        String line = fileReader.nextLine();
        System.out         


        
7条回答
  •  既然无缘
    2020-12-17 06:42

    The others told you enough about your issue with multiple calls of readLine().

    I just wanted to leave sth about code style: While you see this line = assignement and != null check together in one while condition in most examples (like @gotomanners example here) I prefer using a for for it.

    It is much more readable in my opinion ...

    for (String line = in.readLine(); line != null; line = in.readLine()) {
        ...
    }
    

    Another nice way to write it you see in @TheCapn's example. But when you write it that way you easily see that's what a for-loop is made for.

    I do not like assignments scrambled with conditions in one line. It is bad style in my opinion. But because it is so MUCH popular for that special case here to do it that way I would not consider it really bad style. (But cannot understand who established this bad style to become that popular.)

提交回复
热议问题