Vetterjack
Vetterjack

Reputation: 2505

Read 2 Bytes from 23GB file

Is there an easy and fast way to read two bytes from a 23gb large file in java? The Problem is that read() method (FileInputStream) only supports int as offset. Reading chunks of size 2GB in memory takes to long... There musst be a way to skip let's say 15.000.000.000 bytes?

Upvotes: 3

Views: 97

Answers (2)

Jean Logeart
Jean Logeart

Reputation: 53829

Using FileChannel and ByteBuffer:

ByteBuffer bb = ByteBuffer.allocate(1);
FileChannel.open(file.toPath()).position(15e9).read(bb);
byte b = bb.get();

Upvotes: 3

Toilal
Toilal

Reputation: 3409

Use nio FileChannel position(long newPosition) method. To get it, call getChannel() on the good old FileInputStream instance.

Upvotes: 6

Related Questions