Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FileInputStream vs. Random Access File (Read Performance)

 
Mark Mescher
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello out there,
I have a little question to Random Access Files (I have never used them since now).
I have to read very large files, since now I'm doing this with FileInputStreams. Now I have the problem that in some cases I dont need the first bytes (Megabytes...Gigabytes...very large files) of a file.

Which solution would you prefer? I could use a Inputstream and fast forward in this stream the first bytes or alternatively I could use a Random Access File and jump directly to the startpoint I need. Which way is more performant?
Thanx
Bye
Mark
 
Joe Ess
Bartender
Posts: 9370
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RandomAccessFile.seek() will be several times faster than FileInputStream.skip(). This is because RandomAccessFile moves the file pointer directly to the requested offset whereas FileInputStream will read and discard all the data up to the offset.
 
Mark Mescher
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK thank you than I will try it that way.
Bye
Mark
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!