前两种是不带缓冲的的字节流复制,后两种是带缓冲的字节流复制,可以看出带缓冲的字节流复制的效率远远大于不带缓冲的字节流复制,而带字节数组复制的效率也要比单个字节复制的效率高。
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
copy_4();
long e = System.currentTimeMillis();
System.out.println(e-s);
}
/**
* @author admin
* @throws IOException
* @Data 2018-8-3
* @see 字节流复制文件 47776ms
*/
public static void copy_1() throws IOException {
FileOutputStream fos = new FileOutputStream(new File("E:\\demo.java"));
FileInputStream fis = new FileInputStream(new File("D:\\demo.java"));
int n = 0;
while ((n = fis.read()) != -1) {
fos.write(n);
}
fis.close();
fos.close();
}
/**
* @author admin
* @throws IOException
* @see 字节流字节数组复制 83ms
*/
public static void copy_2() throws IOException {
FileOutputStream fos = new FileOutputStream(new File("E:\\demo.java"));
FileInputStream fis = new FileInputStream(new File("D:\\demo.java"));
byte[] bytes = new byte[1024];
int n =0;
while ((n = fis.read(bytes)) != -1) {
fos.write(bytes, 0, n);
}
fis.close();
fos.close();
}
/**
* @author admin
* @throws IOException
* @see 字节流带缓冲复制 272ms
*/
public static void copy_3()throws IOException{
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(new File("E:\\demo.java")));
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(new File("D:\\demo.java")));
int n = 0;
while((n = bis.read()) != -1){
bos.write(n);
}
bos.close();
bis.close();
}
/**
* @author admin
* @throws IOException
* @see 字节流带缓冲字节数组复制 19ms
*/
public static void copy_4()throws IOException{
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(new File("E:\\demo.java")));
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(new File("D:\\demo.java")));
byte[] bytes = new byte[1024];
int n = 0;
while((n = bis.read(bytes)) != -1){
bos.write(bytes,0,n);
}
bos.close();
bis.close();
}