Requirements: Copy of the implementation file
Practice Purpose:
Understand Javanio knowledge, mainly focus on the relationship between Fileinputstream,filechannel,fileoutputstream,bytebuffer
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:
Flow of data:
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/A6/10/wKioL1nIoFzhzlyJAAAtTtCepXQ467.png "title=" 001. PNG "alt=" Wkiol1niofzhzlyjaaatttcepxq467.png "/>
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