ExplicitChannelWrite.java Source code

Java tutorial

Introduction

Here is the source code for ExplicitChannelWrite.java

Source

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

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

        try {
            fOut = new FileOutputStream("test.txt");
            fChan = fOut.getChannel();
            mBuf = ByteBuffer.allocateDirect(26);
            for (int i = 0; i < 26; i++)
                mBuf.put((byte) ('A' + i));
            mBuf.rewind();
            fChan.write(mBuf);
            fChan.close();
            fOut.close();
        } catch (IOException exc) {
            System.out.println(exc);
            System.exit(1);
        }
    }
}