NIO

In this chapter you will learn:

  1. What is Java NIO
  2. Buffers in Java NIO
  3. What are Channels
  4. Charsets and Selectors

use Java NIO

The Java NIO(New I/O) system is built on two foundational items: buffers and channels.

A buffer holds data. A channel represents an open connection to a file or a socket.

To use the NIO system, you obtain a channel to a file and a buffer to hold data. You then operate on the buffer.

Buffers

Buffers are defined in the java.nio package.

All buffers are subclasses of the Buffer class. Buffer class defines the core functionality common to all buffers: current position, limit, and capacity.

The following specific buffer classes are derived from Buffer.

  • ByteBuffer
  • CharBuffer
  • DoubleBuffer
  • FloatBuffer
  • IntBuffer
  • LongBuffer
  • MappedByteBuffer
  • ShortBuffer

MappedByteBuffer is a subclass of ByteBuffer that is used to map a file to a buffer.

Channels

Channels are defined in java.nio.channels. A channel represents an open connection to an I/O source. You obtain a channel by calling getChannel( ) on an object that supports channels.

getChannel( ) is supported by following I/O classes.

  • DatagramSocket
  • FileInputStream
  • FileOutputStream
  • RandomAccessFile
  • ServerSocket
  • Socket

Charsets and Selectors

A charset defines the way that bytes are mapped to characters.

You can encode a sequence of characters into bytes using an encoder. You can decode a sequence of bytes into characters using a decoder. Charsets, encoders, and decoders are supported by classes defined in the java.nio.charset package.

selectors can perform I/O through multiple channels.

Next chapter...

What you will learn in the next chapter:

  1. How to use FileChannel
  2. How to create a FileChannel for reading and writing
  3. How to get the size of a FileChannel
Home » Java Tutorial » NIO
NIO
FileChannel
FileChannel position
FileChannel lock
FileChannel read
FileChannel write