Search code examples
javaniofilechannel

Is there a way to read and write using the same FileChannel?


I am new to Java NIO. I am seeing that a FileChannel object has both read and write methods. But I am unable to read and write using the same FileChannel at a single point of time. Is there a way to do so?


Solution

  • Get a FileChannel from RandomAccessFile object with "rw" mode.

    RandomAccessFile aFile     = new RandomAccessFile("abc.txt", "rw");
    FileChannel      inChannel = aFile.getChannel();
    

    You can refer this link for more. FileChannel tutorial