Java Fundamentals Enhanced IO flow note 76:nio data transfer between channels (channel)

Source: Internet
Author: User

1. In Java NIO, if one of the two channels is FileChannel, you can transfer the data directly from one channel (translator note: Channel Chinese translation channels) to another channel.

(1) transferfrom ():

The Transferfrom () method of the FileChannel can transfer data from the source channel to the filechannel (Translator Note: This method is interpreted in the JDK document as the transfer of bytes from a given readable byte channel to a file in this channel). The following is a simple example:

1Randomaccessfile FromFile =NewRandomaccessfile ("FromFile.txt", "RW");2FileChannel Fromchannel =Fromfile.getchannel ();3 4Randomaccessfile ToFile =NewRandomaccessfile ("ToFile.txt", "RW");5FileChannel Tochannel =Tofile.getchannel ();6 7 LongPosition = 0;8 LongCount =fromchannel.size ();9 TenTochannel.transferfrom (position, count, Fromchannel);

The input parameter of the position method means that the data is written to the destination file starting at position , andcount represents the maximum number of bytes transferred . If the remaining space of the source channel is less than count bytes, the number of bytes transferred is less than the number of bytes requested .
Also note that in the Soketchannel implementation, Socketchannel only transmits the data that is prepared at this point (which may be less than count bytes). Therefore, Socketchannel may not transfer all of the requested data (count bytes) to FileChannel.

(2) TransferTo ():

The TransferTo () method transfers data from the FileChannel to other channel . The following is a simple example:

1 Randomaccessfile FromFile =NewRandomaccessfile ("FromFile.txt", "RW");2FileChannel Fromchannel =Fromfile.getchannel ();3 4Randomaccessfile ToFile =NewRandomaccessfile ("ToFile.txt", "RW");5FileChannel Tochannel =Tofile.getchannel ();6 7 LongPosition = 0;8 LongCount =fromchannel.size ();9 TenFromchannel.transferto (position, count, Tochannel);

Did you find this example particularly similar to the previous example? Except that the FileChannel object that invokes the method is different, the others are the same.
The above-mentioned question about Socketchannel also exists in the Transferto () method. The Socketchannel will transmit the data until the target buffer is filled.

Java Fundamentals Enhanced IO flow note 76:nio data transfer between channels (channel)

Related Article

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.