本篇内容主要讲解“如何测试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往文件中写入数据”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。