Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read a file line by line?  RSS feed

 
Gerald Luckenbill
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
 
jason adam
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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!