gnreddy
gnreddy

Reputation: 2503

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?

Upvotes: 0

Views: 1224

Answers (2)

UVM
UVM

Reputation: 9914

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

Upvotes: 2

user207421
user207421

Reputation: 310850

If you obtain the FileChannel from a RandomAccessFile opened in "rw" mode you can both read and write. In all other cases you can't.

Upvotes: 0

Related Questions