如何测试FileChannel结合MappedByteBuffer往文件中写入数据
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇内容主要讲解"如何测试FileChannel结合MappedByteBuffer往文件中写入数据",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何测试
千家信息网最后更新 2025年12月02日如何测试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安全错误
数据库的锁怎样保障安全
考教师计算机网络技术
天骐怪物塔防服务器
mc联机需要开服务器吗
学网络技术培训
微信小程序云开发数据库怎么建立
网络安全公司需要
服务器电源识别电压
什么叫数据库可扩展性
广州电脑软件开发价钱
浙江嵌入式软件开发收费报价表
数据库专家服务
办公室电脑网络安全要求
网络安全认证服务机构
招聘软件开发程序员
网络技术组考试题
苏州科技园有互联网公司
郴州市网络安全知识竞赛通知
网络安全工程师去哪报名
十堰串口服务器多少钱
网络安全事件分级分类指南
万方数据库服务内容
达芬奇是否一定要建数据库
苏宁 软件开发
和服务器通讯失败
网络安全预测2021
微信商城软件开发
大连鹏升互联网科技有限公司
鲲鹏软件开发团队
办公室电脑网络安全要求
天津数据库审计项目