Java NIO Copy File case

Source: Internet
Author: User


Requirements: Copy of the implementation file


Practice Purpose:

    1. Understand Javanio knowledge, mainly focus on the relationship between Fileinputstream,filechannel,fileoutputstream,bytebuffer

    2. Learn how to get FileChannel

Public class copyfile {    public static void main (String[]  args)  throws Exception {        String  infile =  "GitHub.txt";         string outfile =   "GitHub2.txt";                 //gets the input stream and output stream of the source and destination files         fileinputstream fin =  new fileinputstream (InFile);        fileoutputstream  Fout = new fileoutputstream (OutFile);                 //get input, output channel          Filechannel fcin = fin.getchannel ();         filechannel  fcout = fout.getchannel();                 //Create buffers         bytebuffer buffer = bytebuffer.allocate (1024);                 while  ( True)  {                     //clear buffer data, can receive new data              buffer.clear ();                     //reading data from input channels to buffers              int r = fcin.read (buffer);         The             //read method returns the number of bytes read, possibly 0,  Returns -1   &NBSP if the channel has reached the end of the stream;        if  (r == -1)  {                 break;             }            The          //flip method allows the buffer to write the newly read data to another channel              buffer.flip ();                     //from buffer   write data to   output channel              fcout.write (buffer);         }                 //Last Close         fcin.close ();         fout.clOSE ();     }} 


Summarized as follows:

    1. Flow of data:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/A6/10/wKioL1nIoFzhzlyJAAAtTtCepXQ467.png "title=" 001. PNG "alt=" Wkiol1niofzhzlyjaaatttcepxq467.png "/>

    1. When do I call FileChannel's Read,write method?

Determine according to the direction of the data

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/A6/10/wKioL1nIoRqjzAD7AABkbNmoa2E915.png "title=" 002. PNG "alt=" Wkiol1niorqjzad7aabkbnmoa2e915.png "/>














This article is from the "Xej Distributed Studio" blog, so be sure to keep this source http://xingej.blog.51cto.com/7912529/1968417

Java NIO Copy File case

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.