"Java" "NiO" 3, Java NIO Channel

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.