aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""un-reading" a line" Watch ""un-reading" a line" New topic
Author

"un-reading" a line

Tommy Mato
Greenhorn

Joined: Dec 14, 2007
Posts: 26

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

Joined: Oct 29, 2001
Posts: 8705
    
    6

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
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 !


Discussion - the powerfull way to excellence!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3156
    
  10
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 ?


Joanne
Tommy Mato
Greenhorn

Joined: Dec 14, 2007
Posts: 26

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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.



[Jess in Action][AskingGoodQuestions]
Tommy Mato
Greenhorn

Joined: Dec 14, 2007
Posts: 26

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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Dec 14, 2007
Posts: 26

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "un-reading" a line
 
Similar Threads
Help with basic syntax
Help! Need to write out a amount after string has certain values in it. Must add and read file.
StreamTokenizer vs StringTokenizer
reading the second word from a string
Writing to a file