Home  >  Core Java

Example of MappedByteBuffer in Java

By Arvind Rai, November 17, 2013
MappedByteBuffer is the API of java.nio package. MappedByteBuffer is the direct byte buffer. MappedByteBuffer forces the content into file. MappedByteBuffer can change the file. FileChannel.map returns the object of MappedByteBuffer.

MappedByteBufferTest.java
package com.concretepage.nio;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MappedByteBufferTest {
	public static void main(String[] args) {
		try{
		    RandomAccessFile rafile = new RandomAccessFile("C:/temp/test.txt", "rw");
		    FileChannel fileChannel = rafile.getChannel();
		    MappedByteBuffer mbb = fileChannel.map( FileChannel.MapMode.READ_WRITE,0, 1024 );
		    String msg = "Hellow World!";
		    mbb.put(msg.getBytes());
		    mbb.force();
		    fileChannel.close();
		    rafile.close();
		} catch (IOException e) {
	      e.printStackTrace();
        }
	}
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us