天天看点

Java NIO ChannelJava NIO Channel

Java Nio 

1

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/index.html">Java NIO Tutorial</a>

2

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/overview.html">Java NIO Overview</a>

3

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/channels.html">Java NIO Channel</a>

4

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/buffers.html">Java NIO Buffer</a>

5

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/scatter-gather.html">Java NIO Scatter / Gather</a>

6

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/channel-to-channel-transfers.html">Java NIO Channel to Channel Transfers</a>

7

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/selectors.html">Java NIO Selector</a>

8

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/file-channel.html">Java NIO FileChannel</a>

9

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html">Java NIO SocketChannel</a>

10

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/server-socket-channel.html">Java NIO ServerSocketChannel</a>

11

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/datagram-channel.html">Java NIO DatagramChannel</a>

12

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/pipe.html">Java NIO Pipe</a>

13

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/nio-vs-io.html">Java NIO vs. IO</a>

Java NIO ChannelJava NIO Channel

Rate article:

Share article:

<a target="_blank" href="https://twitter.com/share">Tweet</a>

Table of Contents

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/channels.html#channel-implementations">Channel Implementations</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/channels.html#basic-channel-example">Basic Channel Example</a>

Java NIO Channels are similar to streams with a few differences:

You can both read and write to a Channels. Streams are typically one-way (read or write).

Channels can be read and written asynchronously.

Channels always read to, or write from, a Buffer.

As mentioned above, you read data from a channel into a buffer, and write data from a buffer into a channel. Here is an illustration of that:

Java NIO ChannelJava NIO Channel

Java NIO: Channels read data into Buffers, and Buffers write data into Channels

<a target="_blank"></a>

Here are the most important Channel implementations in Java NIO:

FileChannel

DatagramChannel

SocketChannel

ServerSocketChannel

The <code>FileChannel</code> reads data from and to files.

The <code>DatagramChannel</code> can read and write data over the network via UDP.

The <code>SocketChannel</code> can read and write data over the network via TCP.

The <code>ServerSocketChannel</code> allows you to listen for incoming TCP connections, like a web server does. For each incoming connection a <code>SocketChannel</code> is created.

Here is a basic example that uses a <code>FileChannel</code> to read some data into a <code>Buffer</code>:

Notice the <code>buf.flip()</code> call. First you read into a Buffer. Then you flip it. Then you read out of it. I'll get into more detail about that in the next text about <code>Buffer</code>'s.

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/buffers.html"></a>

Next:   Java NIO Buffer