When we use Windows system, we find that the speed of copying a file is generally consistent, we can guess that this is a Microsoft internal staff design an algorithm, and this operation can achieve the maximum speed is what effect??
Then can we design a method to replace Windows with the copy and paste method to achieve faster transmission speed?
The following code implements the file copy work in a buffered situation:
Publicvoid copybyte (String srcfile,string destfile) throws ioexception{
//Create input stream
InputStream ins=new FileInputStream (srcfile);
Create output stream
outputstream outs=new fileoutputstream (destfile);
Create buffer, read 16K file
byte[] buf=newbyte[16384];
int I=ins.read (BUF);
SYSTEM.OUT.PRINTLN ("Start copying ...");
Gets the current time
System.out.println (System.currenttimemillis ());
while (i!=-1) {
outs.write (BUF);
I=ins.read (BUF);
}
System.out.println (System.currenttimemillis ());
System.out.println ("Copy over ...");
Close Flow
ins.close ();
Outs.close ();
}
First of all, I used a program of more than 860 m to test, found that the speed of copying and pasting in Windows is roughly 40M per second, copying one such file takes approximately more than 20 seconds, while using the above method to replicate, after repeated testing, Found that the time is basically about 23 seconds in appearance, and when the value of more than 16K, the speed will not be increased, and adjust to 8K, time is about 29 seconds, in fact, this also explains a phenomenon, the algorithm itself can not exceed the computer hard disk and CPU interaction speed, the hardware is still the bottleneck, But can we use an external faster hard drive to achieve the speed of the original hard drive??
In addition, see the Bufferredinputstream source of all know that there is such a property:
Privatestaticintdefaultbuffersize = 8192;
This means that the buffer input stream defined buffer array size is 8K, and did not reach the computer's limit speed, the computer has more secrets waiting for us to find out?
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/