首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

[java][nio]MappedByteBuffer批改大文件

2012-12-25 
[java][nio]MappedByteBuffer修改大文件import java.io.FileNotFoundExceptionimport java.io.IOExceptio

[java][nio]MappedByteBuffer修改大文件

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;//128mpublic 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();}}

热点排行