How to read a single line from file in java

前端 未结 3 1091
长情又很酷
长情又很酷 2020-12-22 08:41

How can i read single line from a text file in java. and what is the criteria of knowing that line is completed.

secondly

i read file and then for Read Line

3条回答
  •  忘掉有多难
    2020-12-22 09:22

    First thing : readLine() returns String value only so it is not converting to String.

    Second thing : In your while loop, you read firstline and check whether the content of first line is null or not. But when data = infile.readLine(); executes, it will fetch second line from file and print it to Console.

    Change your while loop to this :

    while((data = infile.readLine()) != null){ 
        System.out.println(data); 
    }
    

    If you use toString() method, it will throw NPE when it will try to use toString method with null content read from infile.

提交回复
热议问题