import java.nio.*;
import java.nio.channels.*;
import java.io.*;

public class ChannelTest1 {
    private String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789あいうえお";

    public ChannelTest1(String filename) {
        write(filename);
        read(filename);
    }

    public void write(String filename){
        try{
            FileOutputStream stream = new FileOutputStream(filename);
            FileChannel channel = stream.getChannel();
        
            byte[] bytes = text.getBytes();
            
            ByteBuffer buffer = ByteBuffer.wrap(bytes);

            channel.write(buffer);

            channel.close();
        }catch(FileNotFoundException ex){
            ex.printStackTrace();
        }catch(IOException ex){
            ex.printStackTrace();
        }
    }

    public void read(String filename){
        try{
            FileInputStream stream = new FileInputStream(filename);
            FileChannel channel = stream.getChannel();
        
            ByteBuffer buffer = ByteBuffer.allocate((int)channel.size());
            channel.read(buffer);

            buffer.clear();
            byte[] bytes = new byte[buffer.capacity()];
            buffer.get(bytes);
            System.out.println("Buffer: " + new String(bytes));

            channel.close();
        }catch(FileNotFoundException ex){
            ex.printStackTrace();
        }catch(IOException ex){
            ex.printStackTrace();
        }
    }

    public static void main(String[] args){
        new ChannelTest1(args[0]);
    }
}
