首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

内存映射文件有关问题

2011-12-29 
内存映射文件问题为了加快读写操作,想用内存映射文件写文件s是一个很大的字符串publicstaticvoidwrite(Str

内存映射文件问题
为了加快读写操作,想用内存映射文件写文件
s是一个很大的字符串
public   static   void   write(String   s)   throws   IOException   {
        File   f   =   new   File( "1.txt ");
        f.createNewFile();
        FileChannel   fc   =   new   RandomAccessFile(f,   "rw ").getChannel();
        CharBuffer   cb   =   fc.map(FileChannel.MapMode.READ_WRITE,   0,  
                                          s.length()   *   2).asCharBuffer();
        cb.put(s);
        fc.close();
}
但是这样s中的中文却变成乱码,如何解决???
怎么样写的又快又没有乱码呢????


[解决办法]
String s = "this is 测试 demo ";
File f = new File( "d:\\1.txt ");
f.createNewFile();

FileChannel fc = new RandomAccessFile(f, "rw ").getChannel();
ByteBuffer cb = fc.map(FileChannel.MapMode.READ_WRITE, 0, s.length() * 2);
cb.put(s.getBytes( "gb2312 "));
fc.close();

热点排行