Share on Google+Share on Google+

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

Advertisement
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

Advertisements

Posted on: August 13, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.