• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to read lines in BuffredReader

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I read line by line from a text file making use of BuffredReader.
 
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Do Re Mi Fa So La Tiny Ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic