Java nio package - Learn how to use java nio package.


 

Java nio package - Learn how to use java nio package.

In this section we will learn about the java.nio package.

In this section we will learn about the java.nio package.

Java nio package.

In this section we will learn about the java.nio package. We will also see how to use java.nio
package classes and its methods in java programming.

The java.nio (known as java new input output) package was introduced in Java 1.4 which is broadly 
used in data processing and it also provides better scalability. The benefits of using this is to read 
and write block of data rather byte by byte from the disk. The IO api works with byte streams 
and character stream but NIO api works wtih channel and buffers. A Java NIO FileChannel is 
used to connect to a file. Using a file channel you can read data from a file, and write data to 
a file. Data from channel is read into buffer and likewise it is written from a buffer to a channel.
 Java NIO Buffer is used when interacting with NIO Channels.

The java.nio package contains following abstract classes:

Buffer :
Buffer is basically a container for data of a specific primitive type which also is a linear and finite
sequence of elements. The other important properties of buffer are its capacity, limit and position
which are used while reading and writing into the buffer.

ByteBuffer :
The ByteBuffer class defines methods for reading and writing values of all primitive type (char, short,
int, long, and double) except the boolean type.

CharBuffer :
The Character Buffer is created either allocating space for buffer's content, by wrapping and existing
char array into a buffer or by creating a view of an existing byte buffer.

DoubleBuffer :
The Double Buffer is created either allocating space for buffer's content, by wrapping and existing
double array into a buffer or by creating a view of an existing byte buffer.

FloatBuffer :
The Float Buffer is created either allocating space for buffer's content, by wrapping and existing float
array into a buffer or by creating a view of an existing byte buffer.

IntBuffer :
The int Buffer is created either allocating space for buffer's content, by wrapping and existing int array
into a buffer or by creating a view of an existing byte buffer.

LongBuffer :
The long buffer is created either allocating space for buffer's content, by wrapping and existing long
array into a buffer or by creating a view of an existing byte buffer.

MappedByteBuffer :
The Mapped byte buffers is created with the map method of FileChannel class available in
java.nio.channels package. The contents of a mapped byte buffer can be changed at any time.
If the mapped file is truncated then all or part of a mapped byte buffer may be inaccessible at any
time.

ShortBuffer :
The short buffer is created either allocating space for buffer's content, by wrapping and existing
shortarray into a buffer or by creating a view of an existing byte buffer.

Now we will see examples on java.nio package and how it is implemented in java programming.

Ads