MappedChannelWrite.java Source code

Java tutorial

Introduction

Here is the source code for MappedChannelWrite.java

Source

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class MappedChannelWrite {
    public static void main(String args[]) {
        RandomAccessFile fOut;
        FileChannel fChan;
        ByteBuffer mBuf;

        try {
            fOut = new RandomAccessFile("test.txt", "rw");
            fChan = fOut.getChannel();
            mBuf = fChan.map(FileChannel.MapMode.READ_WRITE, 0, 26);
            for (int i = 0; i < 26; i++)
                mBuf.put((byte) ('A' + i));

            fChan.close();
            fOut.close();
        } catch (IOException exc) {
            System.out.println(exc);
            System.exit(1);
        }
    }
}