Buffered stream implementation file copy, can operate on the pictures, video, zip, rar and other documents
Package com.io.buffered;
Import java.io.BufferedInputStream The;
Import java.io.BufferedOutputStream;
Import a java.io.FileInputStream;
Import java.io.FileOutputStream;
Import java.io.IOException;
Import org.junit.Test;
/ **
* implemented using a buffered stream file Copy
* /
public class BufferedStreamFile {
// non-text files put Copy
@Test
public void copyFileTest () {
// time in the recording
Long Start = System.currentTimeMillis ();
String the src= "C: \\ Desktop the Users \\ \\ \\ 1.png Administrator" ;
String dest = "C: \\ Desktop the Users \\ \\ \\ 2.png Administrator" ;
copyFile (the src, dest);
/ / end time
Long end = System.currentTimeMillis ();
System.out.println ( "Processed:" + (end - Start));
}
public static void copyFile (the src String, String dest) {
// . 3, record BufferedInputStream packaging stream corresponding node, for improving the efficiency
BufferedInputStream BIS = null ;
// . 4, the node corresponding to the package to create BufferedOutputStream stream, for improving the efficiency
BufferedOutputStream BOS = null;
The try {
// . 1, to create the FileInputStream
the FileInputStream FIS = new new the FileInputStream (the src);
// 2, creates FileOutputStream
a FileOutputStream fos = new new a FileOutputStream (dest);
BIS = new new BufferedInputStream (FIS);
BOS = new new BufferedOutputStream The (fos);
/ / 5, reads the corresponding content file
byte [] B = new new byte [1024 ];
int len = 0 ;
the while((len = bis.read (B)) = -1! ) {
// . 6 reads the contents written to the target location
bos.write (B, 0 , len);
}
// bos.flush (); // [mandatory where the buffer to prevent data loss, in general, do not use] was added
} the catch (IOException E) {
e.printStackTrace ();
}
// . 7, closing the flow
IF (! BOS = null ) {
the try {
bos.close ( );
} the catch (IOException E) {
e.printStackTrace ();
}
}
IF (BIS =!null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}