我想將我的內容數據寫入文件的每個10kb文件。它看起來像這樣: 將數據更新到文件的每個字節數
我試了一下:
FileInputStream is;
FileOutputStream out;
File input = new File(filePath);
int fileLength = input.length();
int len = 0;
while (len < fileLength){
len += is.read(buff);
// write my data
out.write(data, 0, data.length);
// how to move is to read next 10kb???
}
我想反正是有光標閱讀機移動到字節的下一個量?或者我錯過任何東西?
更新:謝謝給@DThought
,這裏是我的實現:
File input = new File(filePath);
long fileLength = input.length();
byte[] data;
byte[] buff = new byte[data.length];
long JUMP_LENGTH = 10 * 1024;
RandomAccessFile raf = new RandomAccessFile(input, "rw");
long step = JUMP_LENGTH + data.length;
for (long i = 0; i < fileLength; i += step) {
// read to buffer
raf.seek(i);
raf.read(buff);
raf.seek(i); // make sure it move to correct place after reading
raf.write(data);
}
raf.close();
,效果不錯。