Programming And Stuff, You Know The Thing…

Seeking and File Input Streams in Java

Posted at — Mar 20, 2013

If you want to copy a specific chunk out of a file, the stream solutions will read the skipped bytes at the start of the file. The solution is to use a file descriptor:

RandomAccessFile file = new RandomAccessFile(localFile, "r");
IOUtils.copyLarge(new FileInputStream(file.getFD()),
        resp.getOutputStream(), 0L, endPos - startPos + 1);