![](/img/trans.png)
[英]Is the method read(byte b[], int off, int len) in InputStream same as the method read(byte b[], int off, int len) in FileInputStream?
[英]JAVA: Byte array allocation in InputStream.read(byte[] b, int off, int len)
我有一個大文件,希望從該文件中將字節分成20,000,000字節的塊。
我寫了這樣的代碼:
File clipFile = new File("/home/adam/Temp/TheClip.mp4");
InputStream clipIStream = new FileInputStream(clipFile);
long chunkSize = 20000000;
long fileSize = clipFile.length();
long totalParts = (long) Math.ceil(fileSize / chunkSize);
for(int part=0; part < totalParts; part++) {
long startOffset = chunkSize * part;
byte[] bytes = new byte[(int)chunkSize];
clipIStream.read(bytes, (int) startOffset, (int) chunkSize));
// Code for processing the bytes array
// ...
}
程序在第一次迭代后崩潰,生成IndexOutOfBoundsException
。
public int read(byte [] b,int off,int len)拋出IOException
(......)
讀取的第一個字節存儲在元素b [off]中,下一個字節存儲在b [off + 1]中,依此類推。
這意味着,在第二次迭代中, read
開始在位置byte [20000000]上寫入,而不是我想要的bytes [0]。
是否有任何方法可以在每次迭代時在字節數組的開頭寫入字節?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.