Write to a file using the new I/O : New IO « File Input Output « Java

Write to a file using the new I/O

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

public class MainClass {
  public static void main(String args[]) {
    FileOutputStream fileOutputStream;
    FileChannel fileChannel;
    ByteBuffer byteBuffer;

    try {
      fileOutputStream = new FileOutputStream("test.txt");
      fileChannel = fileOutputStream.getChannel();
      byteBuffer = ByteBuffer.allocateDirect(26);

      for (int i = 0; i < 26; i++)
        byteBuffer.put((byte) ('A' + i));
    } catch (IOException exc) {


Related examples in the same category

1.Use a mapped file to read a text file
2.FileChannel: map(FileChannel.MapMode mode,long position,long size)
3.Write to a mapped file
4.Copy a file using NIO
5.Write A String As Bytes
6.Use New Java IO to write string into a file