Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to read lines in BuffredReader  RSS feed

 
Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I read line by line from a text file making use of BuffredReader.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this´┐Ż



String str=null;



while((str= reader.readLine())!=null){

System.out.println("String is "+str);

}
 
Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My text file format is

aaaaaaaaaaaaa
bbbbbbbbbbbbb
ccccccccccccc

so I have to read line by line

my doubt is after reading one line(using BufferedReader.readline()) how can we navigate to the second line??
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileReader fr = new FileReader("FilereadingLinesTxt.txt");
BufferedReader br = new BufferedReader(fr);
String str=null;

while((str=br.readLine())!=null){

System.out.println("String is "+str);

}

Output:
String is aaaaaaaaaaaaa
String is bbbbbbbbbbbbb
String is ccccccccccccc
[ October 31, 2006: Message edited by: A Kumar ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sudhana madhu:
my doubt is after reading one line(using BufferedReader.readline()) how can we navigate to the second line??

Each time you call readLine(), you are getting the next line. You don't need to do anything to navigate to the next line, just call readLine() in a loop until it returns null (which means you're at the end of the file), as A Kumar showed you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!