| Author |
How to randomly access a file
|
Craig Taylor
Ranch Hand
Joined: Jul 17, 2008
Posts: 64
|
|
After creating a file reference, I'd like to read a header and then at various times seek to various positions (based upon the contents of the header). Using reset() fails to work as apparently <code>new InputStream( new File( filename ) )</code> doesn't implement marking. Short of destroying the input stream and re-creating it how is this typically handled? Thanks,
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8290
|
|
|
java.io.RandomAccessFile
|
"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
|
 |
Craig Taylor
Ranch Hand
Joined: Jul 17, 2008
Posts: 64
|
|
Thanks! - I found it shortly after I posted the question (always _after_ you've looked but before you ask... ). Is anyone aware of why this is not wrapped into the other InputStream functionality? The library organization just seems... broken in this regard. Thanks,
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
Well, an input streams is... well... a stream. You can't really wrap a random access file with a buffer and a print formatter, if the source isn't exactly a stream. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Craig Taylor
Ranch Hand
Joined: Jul 17, 2008
Posts: 64
|
|
The streaming I/O functions do take the impression that most of the operations are sequential in nature. However, the addition of mark() and reset() indicate that some I/O Streams should allow random access type operations. The mark() and reset() operations are not strictly random-access but properly implemented, they could have been without impacting the IOStreams. In my thinking, normally streams go forward but streams (think mag-tapes) should also be allowed to be positioned and then read and advanced from that point.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
But which would RandomAccessFile need to extend? InputStream or OutputStream? Perhaps even Reader or Writer? Because it clearly has support for all four. That does expose another flaw with InputStream, OutputStream, Reader and Writer - they are abstract classes. They could (and perhaps should) have been interfaces, with basic implementations like AbstractInputStream etc. Unfortunately, the first version of the API was far from well designed. Too few interfaces, too many abstract classes. And Sun are now unable to change it, since it would break a lot of code. That explains the high usage of Hashtable and Vector - all in older classes before Java 1.2, and now unable to change.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: How to randomly access a file
|
|
|