Home Tutorial Java Corejava Nio Java MappedByteBuffer example, How to create mapped byte buffer in java.

 
 

Java MappedByteBuffer example, How to create mapped byte buffer in java.
Posted on: August 13, 2010 at 12:00 AM
In this tutorial, you will see how to create mapped byte buffer in java.

Java MappedByteBuffer example, how to create mapped byte buffer in java.

In this tutorial, you will see how to create mapped byte buffer in java.

Code

import java.nio.*;
import java.nio.channels.FileChannel;
import java.io.*;

public class CreateMapBuffer {
  public static void main(String[] argsthrows IOException  {    
File file=new File("FileTest.txt");
FileInputStream fin=new FileInputStream(file);
FileChannel fc=fin.getChannel();
MappedByteBuffer mapByteBuff=fc.map(FileChannel.MapMode.READ_ONLY,
 
0, file.length() );
 while (mapByteBuff.hasRemaining())
 {
   System.out.print((char)mapByteBuff.get());
}
 System.out.println();
 if(mapByteBuff.isReadOnly())
 {
   System.out.print("MappedByteBuffer is read only");
 }else
 {
   System.out.print("MappedByteBuffer is not read only");
 }
}
}

Output

C:\>java CreateMapBuffer
RoseIndia
MappedByteBuffer is read only

Download this code

Related Tags for Java MappedByteBuffer example, How to create mapped byte buffer in java.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.