考虑以下代码:
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("/home/gala/falken_test/test_file"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("/home/gala/falken_test/out"));
int b;
byte[] buff = new byte[8096];
while ((b = bufferedInputStream.read(buff)) > -1) {
bufferedOutputStream.write(buff, 0, b);
System.out.println("Writing " + b);
}
bufferedInputStream.close();
bufferedOutputStream.close();
这样一次将文件从A点(bufferedInputStream
)复制到B点(bufferedOutputStream
),8096
个字节,直到最后一次尝试大于文件本身,然后它修剪缓冲区并读取剩余数据。这看起来像这样,这是System.out.println("Writing " + b);
Writing 8096
Writing 8096
...
Writing 8096
Writing 8096
Writing 5681
This took 14 ms
现在问题出现了,我怎样才能复制一定范围的字节?
例如,从1012
字节到2134
,例如,如果文件长度为10000
个字节。
我写的是一个tar实现,其中许多文件被一个接一个地连接在一起,它们之间有一个512
字节头,它描述了头后面的字节。
我读取标题,跳过标题的512字节并开始写入文件,直到标题中指定的n字节数据。
如何在不逐字节复制的情况下解决这个问题?
答案 0 :(得分:1)
您可以使用BufferedInputStream.skip(long)
跳到右侧pos,然后读取和写入从1012
(startPos
)到2134
(endPos
的字节数}),即2134-1012=1122
个字节。
注意:我将缓冲区大小更改为512,以显示它如何与您的示例位置一起使用。
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("test_file"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("test_out"));
int CHUNK_SIZE = 512; // change to 8096
int startPos = 1012;
int endPos = 2134;
int bytesToRead = endPos - startPos;
int b;
byte[] buff = new byte[CHUNK_SIZE];
bufferedInputStream.skip(startPos - 1);
int currentChunkSize = Math.min(CHUNK_SIZE, bytesToRead);
while ((b = bufferedInputStream.read(buff, 0, currentChunkSize)) != -1) {
bufferedOutputStream.write(buff, 0, b);
System.out.println("Writing " + b);
bytesToRead -= b;
if (bytesToRead == 0) {
break;
}
currentChunkSize = Math.min(CHUNK_SIZE, bytesToRead);
}
bufferedInputStream.close();
bufferedOutputStream.close();
这个输出是:
$ java -cp ReadWriteTest
Writing 512
Writing 512
Writing 98