• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to randomly access a file  RSS feed

 
Craig Taylor
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9439
12
Linux Mac OS X Windows
 
Craig Taylor
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Craig Taylor
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!