bytebuffer ByteBuffers FileChannel java 7 java nio.2 NIO SeekableByteChannel

How to use SeekableByteChannel interface for Random Access to Files (RAF) – andreINC

RAFs, Random Access Files permit asynchronous (random) access to a file contents. To access a file randomly we open the file, search a specific place, and then we learn or write to that file.

Java NIO.2 introduces a new interface – SeekableByteChannel for working with Random Access Files. Also improves the well-known FileChannel class by implementing this interface.

Before we begin to speak about about FileChannel and SeekableByteChannel it’s advisable to first speak somewhat bit about ByteBuffers and Channels.


A byte-buffer is an in-memory array of bytes. It often accommodates knowledge that was just lately learn, or that can be written from/to a vacation spot.

A buffer has three necessary properties:

  • The buffer’s capacity represents the “maximum amount of information” that can be saved within the buffer.
  • The buffer’s position represents how a lot knowledge has been read or written. The place is an index within the buffer’s array, and can’t have a destructive worth or a worth greater than the buffer’s capability.
  • The buffer’s restrict is the difference between buffer’s capability and the buffer’s position.


Channels are in a approach comparable with the classical I/O streams, the distinction is that whereas streams are one-way directed (learn or write), channels can help each operations in the identical time. Additionally Channels permit you to write and skim asynchronously.

Using SeekableByteChannel to write / read to / from information

The SeekableByteChannel interface has 6 strategies:

  • position(): Returns the channels present place.
  • position(lengthy): Units the channel position to the required value. The worth wants to be a constructive quantity.
  • truncate(lengthy): Truncates the entity related to the SeekableByteChannel to the required worth.
  • read(ByteBuffer): Reads into the buffer (from the channe).
  • write(ByteBuffer): Writes bytes from the buffer to the channel.
  • measurement(): Returns the present measurement of the entity to which the channel is related.

To open a SeekableByteChannel we’ll need to use the the 2 methods from the java.nio.file.Files class:


As you’ll be able to see to open a seekable channel you want to supply as input the Path you want to open (principally the file) and in addition open options, that are enum constants.

The potential enum options are as comply with:

READThe file is opened with READ entry.
WRITEThe file is opened with WRITE entry.
CREATECreates the file if the file does not already exist.
CREATE_NEWCreates the file if the file doesn’t exist already. If the file exists throw an exception.
APPPENDAppends to the file. It’s used together with CREATE and WRITE.
DELETE_ON_CLOSEDeletes the file after the channel is closed. Use this when creating and modifying / studying from momentary information.
TRUNCATE_EXISTINGTruncates the file to measurement 0. It’s used along side WRITE and it’s useful whenever you want to clear the contents of a file.
SPARSEOften used along side CREATE or CREATE_NEW. On some file methods giant information which have massive “data gaps” are saved in a more efficient means.
SYNCFile content material + metadata is synchronized with the underlying file system.
DSYNCFile content material is synchronized with the underlying file system.

Reading a file utilizing a SeekableByteChannel

Please comply with the feedback:


Writing a file utilizing SeekableByteChannel

Please comply with the feedback:

If we are going to open the file referred to as “file2.txt” from our residence folder the content might be:

If we run a number of occasions this system the content of the file will appear to be this (the text is appended):

Using the SeekableByteChannel to read characters from totally different file places

Given the following acrostic:

We would like to learn the first 3 letters at first of each phrase and print on the usual output.

Code instance:

And the output of the code: