How to test FileChannel and MappedByteBuffer to write data to a file
This article focuses on "how to test FileChannel and MappedByteBuffer to write data to a file", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to test FileChannel and MappedByteBuffer to write data to a file.
FileChannel combined with ByteBuffer testing
Package com;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;/** * FileChannelTest * * @ 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 combined with ByteBuffer testing
Package com;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;/** * MappedByteBufferTest * * @ 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;}} so far, I believe you have a better understanding of "how to test FileChannel and MappedByteBuffer to write data to a file". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!