2012-05-03 31 views
2

我想用另一个字节数组“覆盖”或“切换”一些字节(比如文件的前2048个字节),大小为100MB。
我不想阅读整个文件,因为需要批评时间才能浏览整个文件。编辑一个文件而不需要遍历整个字节缓冲区?

我试过到目前为止:

FileOutputStream out = new FileOutputStream(file); 

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

新缓冲区数组是相同的尺寸。

我正在用java + eclipse开发Android应用程序,需要这些。
如果有人能给我写一段代码来完成这项工作,我会很高兴。

在此先感谢。

+0

我们不会为您编写代码,但会尝试帮助解决您尝试过的任何操作。 –

+0

@Marc B:谢谢,我已经将它添加到问题的正文 –

+0

这是不可能的。 –

回答

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 
+0

谢谢,我会在一秒钟内尝试,如果真的有效,就会给你名声。 –

+0

@TalKanel:我不确定自己做了什么与你似乎做的不同 - 但这是要做到这一点... –

+0

@TalKanel:我已经做了一个修改来修复一些错别字。 –

相关问题