Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to read a file line by line?  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to be able to read a file, from the file system line by, basically delimited by \n (new line character).
Then I need to get the size of that line in bytes. And then I will do some processing with that line, which is basically appending it to a buffer with some massaging.
What I don't want to do is read the whole file into a String Buffer at one time and then do my parsing with the StringBuffer - the issue is my file could be extremely large and I am supposed to break my processing into chucks when I hit a certain size.
So, in summary, how do I read a file line by line and then get the size in bytes of that line. I am assuming I will use FileReader but I can only find examples that read it in char by char.
Many Thanks
 
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrap the FileReader in a BufferedReader, and use the readLine() method.
Then, with the String, you can do getBytes and get the size of the byte array that is returned.
 
Gerald Luckenbill
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Super!
Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!