如何测试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往文件中写入数据”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
文章标题:如何测试FileChannel结合MappedByteBuffer往文件中写入数据
标题来源:http://cdiso.cn/article/jijgds.html