2

I need an InputStream to read from some nonzero offset in a File. Which is the more performant way to get and position the stream,

InputStream in = new FileInputStream(file);
in.skip(n);

or

RandomAccessFile raf = new RandomAccessFile(file, "r");
InputStream in = Channels.newInputStream(raf.getChannel().position(n));

Or is there a better way that you'd suggest?

Only one stream will be pulled from the file, so there's no benefit from reusing the RandomAccessFile.

dkarp
  • 14,483
  • 6
  • 58
  • 65

1 Answers1

2

Behind the scenes I believe the methods work in exactly the same way (for FileInputStreams which override the default implementation of skip). And thus there being no appreciable difference in performance.

You may be in interested in this other related question.

Community
  • 1
  • 1
Dunes
  • 37,291
  • 7
  • 81
  • 97