In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.