Java MappedByteBuffer example, How to create a large size file in java.


 

Java MappedByteBuffer example, How to create a large size file in java.

In this tutorial, you will see how to create a large size file in java.

In this tutorial, you will see how to create a large size file in java.

Java MappedByteBuffer example, How to create a large file in java.

In this tutorial, you will see how to create a large file with the help of MappedByteBuffer class in java.

Code

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
            
public class CreateFile{
  public static void main(String[] argsthrows Exception {
    RandomAccessFile out = new RandomAccessFile("test.txt""rw");
    FileChannel fc = out.getChannel();
    int length = 0x8FFFAA;
MappedByteBuffer MappByteBuff = fc.map(FileChannel.MapMode.READ_WRITE,
        0, length);
    for (int i = 0; i < length; i++)
    {
      MappByteBuff.put((byte'B');
    }
    System.out.println("File successFully written");
  }
}

Output

C:\>java CreateFile
File successFully written

Download this code

Ads