如何测试FileChannel结合MappedByteBuffer往文件中写入数据
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇内容主要讲解"如何测试FileChannel结合MappedByteBuffer往文件中写入数据",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何测试
千家信息网最后更新 2026年11月03日如何测试FileChannel结合MappedByteBuffer往文件中写入数据
本篇内容主要讲解"如何测试FileChannel结合MappedByteBuffer往文件中写入数据",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何测试FileChannel结合MappedByteBuffer往文件中写入数据"吧!
FileChannel结合ByteBuffer测试
package com;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;/** * FileChannel Test * * @author Dale * @date 2019/10/23 */public class FileChannelTest{ static ByteBuffer writeBuffer = ByteBuffer.allocate(9); static Integer wrotePosition = 0; static Integer committedPosition = 0; static FileChannel fileChannel = null; static { try { RandomAccessFile randomAccessFile = new RandomAccessFile("d://test.log", "rw"); fileChannel = randomAccessFile.getChannel(); }catch (Exception e){ e.printStackTrace(); } } public static void main(String[] args) throws Exception{ appendData("1234"); commit(); appendData("567"); commit(); appendData("89"); commit(); //fileChannel.force(false); } public static void appendData(String msg) throws Exception{ int msgLen = msg.length(); ByteBuffer byteBuffer = writeBuffer.slice(); byteBuffer.position(wrotePosition); ByteBuffer msgStoreItemMemory = ByteBuffer.allocate(msgLen); msgStoreItemMemory.put(msg.getBytes("UTF-8")); byteBuffer.put(msgStoreItemMemory.array(), 0, msgLen); wrotePosition = wrotePosition + msgLen; } public static void commit() throws Exception{ ByteBuffer byteBuffer = writeBuffer.slice(); byteBuffer.position(committedPosition); byteBuffer.limit(wrotePosition); fileChannel.position(committedPosition); fileChannel.write(byteBuffer); committedPosition = wrotePosition; }}MappedByteBuffer结合ByteBuffer测试
package com;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;/** * MappedByteBuffer Test * * @author Dale * @date 2019/10/23 */public class MappedByteBufferTest { static ByteBuffer writeBuffer = ByteBuffer.allocate(9); static Integer wrotePosition = 0; static Integer committedPosition = 0; static FileChannel fileChannel = null; static MappedByteBuffer mappedByteBuffer; static { try { RandomAccessFile randomAccessFile = new RandomAccessFile("d://test.log", "rw"); fileChannel = randomAccessFile.getChannel(); mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 9); }catch (Exception e){ e.printStackTrace(); } } public static void main(String[] args) throws Exception{ appendData("1234"); appendData("567"); appendData("89"); //mappedByteBuffer.force(); } public static void appendData(String msg) throws Exception{ int msgLen = msg.length(); ByteBuffer byteBuffer = mappedByteBuffer.slice(); byteBuffer.position(wrotePosition); ByteBuffer msgStoreItemMemory = ByteBuffer.allocate(msgLen); msgStoreItemMemory.put(msg.getBytes("UTF-8")); byteBuffer.put(msgStoreItemMemory.array(), 0, msgLen); wrotePosition = wrotePosition + msgLen; }}到此,相信大家对"如何测试FileChannel结合MappedByteBuffer往文件中写入数据"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
测试
数据
文件
内容
UTF-8
学习
实用
更深
兴趣
实用性
实际
操作简单
方法
更多
朋友
网站
频道
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
小米移动互联网科技园
互联网新科技
西安纸贵互联网科技有限公司
科技金融是不是互联网金融
深圳天众互联网科技公司
上游互联网科技
狮子座互联网科技培训
栖莱科技互联网股份有限公司
苏州五指互联网络科技有限
互联网保险科技创业
众安科技工业互联网合作
智帮互联网科技公司最近动向
互联网营销推广黑科技
四川互联网展示科技馆设计
互联网科技新闻
兰石爱特工业互联网科技分公司
中国互联网公司的科技投入
唐山双旺合互联网科技产业园
互联网科技创新创业项目
黑龙江互联网科技公司
网络科技有限互联网
金融科技与互联网金融
互联网科技集团
大白互联网科技
互联网科技公司招聘
创业天下互联网科技有限公司
以后互联网后是啥科技
河南住房之家互联网科技
北京军民互联网科技公司
科技互联网企业在行动