2

サイズが100MBのファイルで、一部のバイト(たとえば、ファイルの最初の2048バイト)を別のバイト配列で「オーバーライド」または「切り替え」たい。
ファイル全体を読むのに批判的な時間がかかるので、ファイル全体を読みたくありません。

私がこれまでに試したこと:

FileOutputStream out = new FileOutputStream(file);

out.getChannel().write(buffer, position);

新しいバッファ配列は同じサイズです。

私はそれに必要なjava+eclipseAndroidアプリケーションで開発しています。
誰かが私にその仕事をするコードを書いてくれたら嬉しいです。

前もって感謝します。

4

1 に答える 1

2

これにより、ファイルの最初の2048バイトが配列の内容で上書きされます。data

final RandomAccessFile file = new RandomAccessFile(filename, "rw");
final FileChannel channel = file.getChannel();
final byte[] data = new byte[2048];          // lets say it's got the data you want
final ByteBuffer buff = ByteBuffer.wrap(data);

channel.position(0);                         // (we were already here, but as an example)
channel.write(buff);                         // writes the entire 2028 bytes from buff
channel.force(false);                        // (superfluous if you close() afterwards)
channel.close();                             // close the file descriptor
于 2012-05-03T05:13:00.257 に答える