MappedByteBuffer 高效读写文件
日期: 2019-06-20 分类: 个人收藏 351次阅读
private static final int start = 0;
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("D:/mapper.txt", "rw");
FileChannel fileChannel = raf.getChannel();
String a = "我要写入文件";
MappedByteBuffer mbb = fileChannel.map(FileChannel.MapMode.READ_WRITE, start, a.getBytes().length+1);
mbb.put(a.getBytes());
mbb.flip();
byte[] bb = new byte[mbb.capacity()];
while (mbb.hasRemaining()){
byte b = mbb.get();
bb[mbb.position()]=b;
}
System.out.println(new String(bb));
raf.close();
}
除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog
标签:tips
精华推荐