• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"un-reading" a line

 
Tommy Mato
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My program needs to read and process lines of comma-separated lines in a text file. The program sometimes needs to 'look ahead' at the next line. It would be much simpler to write if I could un-read the previous line that was read. In other words, next time I read a line I will get the one that i'd just un-read. Someone told me they think its possible but didn't know how.

I don't expect anyone to write freebie code for me - but I'd appreciate being pointed in the right direction. Many thanks.
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You did not tell us how you are reading your data, but you could try mark() and reset(). If that doesn't work or isn't supported (they're optional operations) try PushbackReader.
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tommy Mato:
My program needs to read and process lines of comma-separated lines in a text file. The program sometimes needs to 'look ahead' at the next line. It would be much simpler to write if I could un-read the previous line that was read. In other words, next time I read a line I will get the one that i'd just un-read. Someone told me they think its possible but didn't know how.

I don't expect anyone to write freebie code for me - but I'd appreciate being pointed in the right direction. Many thanks.


Hey ! I cannot get your requirement properly !
Would you just explain with a scenario !

The exact inputs and the outputs

i too can work my brain over it !

That would be good intro to using java 1.5 Class PushBackReader.


Your requirement looks a bit different !

May be i should try this !
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's only two lines that you will ever need references to, couldn't you just have two variables pointing at the two lines you need and then reassign those as needed ?
 
Tommy Mato
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. The file is 400,000 lines of CSV values, each line consisting of a currrency price at a certain time. Each line consists of a date/time followed by a currency price.

I need to convert these 'tick' values as they are known (a tick is for any time the price changes) into a summary for each minute. Ticks can occur at any time - so you read the prices until you've gone past a minute's worth. You only know you've processed a minute's worth of values when you've gone into the next minute. At that point, I would like to put the last line (which belongs to the next minute) back into the file; hence the un-read.

I think bufferedReader might be able to do this with it's mark() and reset() methods, but if anyone knows a better way, I'll be all ears. Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tommy,

Just write and use your own readLine() method with line-level pushback capability. Here's a super-simple version that works with only one line of pushback. If you want to allow more than one line, then you could use a Stack to hold the pushed-back lines.

 
Tommy Mato
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Joe Ess. mark() and reset() worked great.

Ernest Friedman-Hill. Thanks for your help, but the pushback method is not refenced anywhere in your code, so I didn't know what the code was supposed to do.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're supposed to call pushBack(s) to "unread' the String s; the next time you call readLine(), s will be returned.
 
Tommy Mato
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,I re-read your code and understand now. I've already got my code working with mark and reset, but I will use your approach next time. Thanks for the tip!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic