我需要读取文件的一部分,从指定的字节位置开始。
我尝试了以下代码:
protected void writePartToStream(final InputStream in, final OutputStream out, long startBytes) {
final byte[] b = new byte[BUFFER_SIZE];
int count = 0;
amountWritten = startBytes;
// skip logic
// how to skip???
do {
// write to the output stream
try {
out.write(b, 0, count);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
amountWritten += count;
System.out.println("Amount writtent=" + amountWritten);
// read more bytes from the input stream
try {
count = in.read(b, (int) amountWritten, b.length);
} catch (IOException e) {
}
} while (count != -1 && !getStatus().equals(Status.cancelled));
// the connection was likely terminated abrubtly if these are not equal
if (!getStatus().equals(Status.cancelled) && getError() == Error.none
&& amountWritten != fileSize) {
setStatus(Status.error);
this.error = Error.connection;
}
}
但是我从未涉及I/O操作,因此不知道如何开始读取指定位置的文件。