Home Tutorial Java Corejava Nio Java MappedByteBuffer example, How to create a large size file in java.

 
 

Java MappedByteBuffer example, How to create a large size file in java.
Posted on: August 13, 2010 at 12:00 AM
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

Related Tags for Java MappedByteBuffer example, How to create a large size file 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.