Java NiO is similar to traffic, but with some differences:
• The channel can read and write, and the stream only supports unidirectional. Read or write
• Asynchronous channel Read
• Channel reader, they are alternating with buffer
The realization of the Tao
The following is the implementation of the most important channel in Java NIO:
· FileChannel
· Datagramchannel
· Socketchannel
· Serversocketchannel
FileChannel reading data from a file or writing files into a file
Datagramchannel Read and write data on the network via UDP
Socketchannel Read and write data on the network via TCP
Serversocketchannel can listen to TCP network connections, just like Web server. A socketchannel is created for each connection.
The primary channel instance
Use FileChannel to read data into buffer
Example code use, is to hope that you can do their own hands-on. Do not copy!
Note the invocation of Method Buffer.flip (). First, read the data into buffer. Then flip, and then read it out.
The following section describes buffer: "java" "NiO" 4, Java NIO Buffer
"Java" "NiO" 3, Java NIO Channel