FileChannel: write(ByteBuffer buffer) : FileChannel « java.nio.channels « Java by API






FileChannel: write(ByteBuffer buffer)

   
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));
      byteBuffer.rewind();
      fileChannel.write(byteBuffer);
      fileChannel.close();
      fileOutputStream.close();
    } catch (IOException exc) {
      System.out.println(exc);
    }
  }
}

           
         
    
    
  








Related examples in the same category

1.FileChannel.MapMode.READ_ONLY
2.FileChannel.MapMode.READ_WRITE
3.FileChannel: close()
4.FileChannel: lock(long position, long size, boolean shared)
5.FileChannel: map(MapMode mode, long position, long size)
6.FileChannel: position()
7.FileChannel: position(long newPosition)
8.FileChannel: read(ByteBuffer dst)
9.FileChannel: size()
10.FileChannel: transferFrom(ReadableByteChannel src, long position, long count)
11.FileChannel: transferTo(long position, long count, WritableByteChannel target)
12.FileChannel: tryLock()
13.FileChannel: write(MappedByteBuffer buffer)