In Java, there are some optimizations in the FileChannel class that improve the efficiency of the transfer, where the Transferto () and Transferfrom () methods allow one channel to be interleaved to another without requiring a buffer to pass the data. Only the FileChannel class has these two methods, so one of the channels in the Channel-to-channel transport must be filechannel. Data cannot be transferred between sock channels, but the socket channel implements Writablebytechannel and Readablebytechannel interfaces, so the contents of the file can be transmitted to a socket channel using the TransferTo () method. Alternatively, you can use the Transferfrom () method to read data directly from a socket channel to a file.
Channel-to-channel transfers can be extremely fast, especially when the underlying operating system provides local support. Some operating systems can transmit data directly without having to pass through user space. This can be a huge help for a lot of data transfer.
Note: If the file to be copied is greater than 4G, you cannot use the Channel-to-channel method directly, instead of using the Bytebuffer, read from the original file channel to the Bytebuffer, and then write the Bytebuffer to the target file channel.
Here is the code to implement a quick copy of a large file:
Import Java.io.File;
Import java.io.IOException;
Import Java.io.RandomAccessFile;
Import Java.nio.ByteBuffer;
Import Java.nio.channels.FileChannel; public class Bigfilecopy {/** * Direct transfer via channel to channel * @param source * @param dest * @throws IOException * /public static void Copybychanneltochannel (string source, String dest) throws IOException {File source_tmp_file = new
File (source);
if (!source_tmp_file.exists ()) {return;
} randomaccessfile source_file = new Randomaccessfile (Source_tmp_file, "R");
FileChannel Source_channel = Source_file.getchannel ();
File Dest_tmp_file = new file (dest);
if (!dest_tmp_file.isfile ()) {if (!dest_tmp_file.createnewfile ()) {source_channel.close ();
Source_file.close ();
Return
} randomaccessfile dest_file = new Randomaccessfile (dest_tmp_file, rw);
FileChannel Dest_channel = Dest_file.getchannel ();
Long left_size = Source_channel.size ();
Long position = 0;
while (Left_size > 0) { Long write_size = Source_channel.transferto (position, left_size, Dest_channel);
Position + = Write_size;
Left_size-= write_size;
} source_channel.close ();
Source_file.close ();
Dest_channel.close ();
Dest_file.close ();
public static void Main (string[] args) {try {long start_time = System.currenttimemillis ();
Bigfilecopy.copybychanneltochannel ("Source_file", "dest_file");
Long end_time = System.currenttimemillis ();
System.out.println ("Copy time =" + (end_time-start_time));
catch (IOException e) {e.printstacktrace ();
}
}
}