ByteBuffer: allocateDirect(int size) : ByteBuffer « java.nio « Java by API






ByteBuffer: allocateDirect(int size)

   
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.ByteBuffer: allocate(int capacity)
2.ByteBuffer: asCharBuffer()
3.ByteBuffer: asDoubleBuffer()
4.ByteBuffer: asFloatBuffer()
5.ByteBuffer: asLongBuffer()
6.ByteBuffer: asShortBuffer()
7.ByteBuffer: capacity()
8.ByteBuffer: clear()
9.ByteBuffer: compact()
10.ByteBuffer: flip()
11.ByteBuffer: get()
12.ByteBuffer: get(byte[] dst, int offset, int length)
13.ByteBuffer: get(int index)
14.ByteBuffer: getChar()
15.ByteBuffer: getDouble()
16.ByteBuffer: getFloat()
17.ByteBuffer: getInt()
18.ByteBuffer: getLong()
19.ByteBuffer: isDirect()
20.ByteBuffer: limit()
21.ByteBuffer: order()
22.ByteBuffer: order(ByteOrder bo)
23.ByteBuffer: position()
24.ByteBuffer: position(int newPosition)
25.ByteBuffer: put(byte b)
26.ByteBuffer: putChar(char ch)
27.ByteBuffer: putDouble(double value)
28.ByteBuffer: putFloat(float value)
29.ByteBuffer: putInt(int i)
30.ByteBuffer: putLong(long value)
31.ByteBuffer: putShort(short value)
32.ByteBuffer: putShort(int index, short value)
33.ByteBuffer: rewind()
34.ByteBuffer: wrap(byte[] array)