import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/**
*
* MappedByteBuffer可用于对大文件的修改.
*
*/
public class LargeMappedFiles {
static int length = 128 * 1024 * 1024;//128m
public static void main(String[] args) throws FileNotFoundException, IOException {
RandomAccessFile file = new RandomAccessFile("d:\\a.txt","rw");
//MappedByteBuffer是特殊的直接缓冲器.必须指定文件的开始位置和文件大小.
MappedByteBuffer out = file.getChannel().map(FileChannel.MapMode.READ_WRITE, file.length(), length);
for(int i=0; i<length; i++){
out.put((byte)'x');
}
System.out.println("Finish writing.");
for(int i = length/2; i<(length/2 + 6); i++){
System.out.println((char)out.get(i));
}
file.close();
}
}
[java][nio]MappedByteBuffer修改大文件
最新推荐文章于 2024-08-30 10:40:01 发布
