Is there a Java command for returning to a previous line? [closed]
If anyone knew, that'd be great if you could tell me.
I think it maybe:
getPreviousLine();
You mean probably something like goto which enables you to go to a previous like of code. If thats what you mean then the answer is no its not possible in Java, you will have to solve this in another way.
If you can give some more information or code maybe you will get some ideas on how to solve this.
If you are referring to reading from files AND you are using java.io.Reader
, not java.util.Scanner
to do your reading, you can try to use the reader's mark()
method if it's supported (should be if you area working with files and not streaming channels like i.e. network). Then, after you mark()
the position (when reading the previous line), if you want to go back to that line after reading a new one just call the reader's reset()
method.
More information here.
Edit: If your lines are not that big, you can just "cache" them, storing the previous line in memory. That is, again, if it's not over the amount of memory you plan your application will be using. Also, there might be another performance issue with this: allocating and de-allocating memory for a lot of lines may cause the garbage collector to jump in more often, thus causing performance to decrease and, if the garbage collector you are using doesn't do memory compacting, you might get some memory fragmentation there (not the case with Java VM 1.4 or 1.5+ I think, but it might be the case for some smaller VMs like Android's Dalvik - haven't checked if its GC might do memory compaction).
I don't know the current status of this project, but I would encourage you to look at something like JCurses ( http://sourceforge.net/projects/javacurses/ ) or JChassis ( http://freshmeat.net/projects/jc_ansiterm/ ).
精彩评论