RandomAccessFile: getChannel() : RandomAccessFile « java.io « Java by API






RandomAccessFile: getChannel()

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

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

    try {
      randomAccessFile = new RandomAccessFile("test.txt", "rw");
      fileChannel = randomAccessFile.getChannel();
      byteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 26);
      for (int i = 0; i < 10; i++)
        byteBuffer.put((byte) ('A' + i));
      fileChannel.close();
      randomAccessFile.close();
    } catch (IOException exc) {
      System.out.println(exc);
      System.exit(1);
    }
  }
}

           
         
    
  








Related examples in the same category

1.new RandomAccessFile(String fileName, String mode)
2.RandomAccessFile: close()
3.RandomAccessFile: getFilePointer()
4.RandomAccessFile: length()
5.RandomAccessFile: read(byte[] b)
6.RandomAccessFile: readBoolean()
7.RandomAccessFile: readByte()
8.RandomAccessFile: readChar()
9.RandomAccessFile: readDouble()
10.RandomAccessFile: readInt()
11.RandomAccessFile: readLine()
12.RandomAccessFile: seek(long pos)
13.RandomAccessFile: write(byte[] b)
14.RandomAccessFile: writeBoolean(boolean v)
15.RandomAccessFile: writeBytes(String s)
16.RandomAccessFile: writeChar(int v)
17.RandomAccessFile: writeChars(String s)
18.RandomAccessFile: writeDouble(double v)
19.RandomAccessFile: writeInt(int v)
20.RandomAccessFile: writeUTF(String str)