Java NiO is similar to streams but with some differences:
• Channels can read and write, while streams only support one way, read or write
• Channels can be read and written asynchronously
• Channel Read and write, all interacting with buffer
Implementation of the Channel
The following are the implementations of the most important channels 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.
Basic Channel Instance
Use FileChannel to read data into buffer
Example code use, is to hope that we 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.
The following section describes the buffer in detail:
"Java" "NiO" 3, Java NIO Channel