In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.