A
A
afna2015-03-08 23:00:11
Java
afna, 2015-03-08 23:00:11

ByteBuffer read-only?

I am learning Java from the book Thinking in Java.
It has this suggestion:

To restrict file access to read -only , explicitly obtain a ByteBuffer using the allocate() static method

The code follows a few pages later. Code snippet:
...
fc = new FileOutputStream("data2.txt").getChannel();
buffer = ByteBuffer.allocate(24);
buffer.asCharBuffer().put("Some text");
fc.write(buffer);
fc.close();
...

What's the use of the allocate() method if I can put "Some text" in the buffer and write its contents to a file?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily, 2015-03-09
@Applez

I suggest reading the documentation and everything will fall into place.

N
Nikita, 2015-03-09
@jkotkot

ByteBuffer#asReadOnlyBuffer()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question