黑马程序员技术交流社区

标题: 比较四种复制效率 [打印本页]

作者: 18263368378    时间: 2016-4-28 22:47
标题: 比较四种复制效率
package kehouzuoye28_5;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class copy_5 {

        public static void main(String[] args) throws IOException {
                long starTime1 = System.currentTimeMillis();
                copy1();
                long endTime1 = System.currentTimeMillis();
                System.out.println(endTime1-starTime1);//1复制完毕     用时:59312毫秒
                long starTime2 = System.currentTimeMillis();
                copy2();
                long endTime2 = System.currentTimeMillis();
                System.out.println(endTime2-starTime2);//2复制完毕  用时110毫秒
                long starTime3 = System.currentTimeMillis();
                copy3();
                long endTime3 = System.currentTimeMillis();
                System.out.println(endTime3-starTime3);//3复制完毕 用时93毫秒
                long starTime4 = System.currentTimeMillis();
                copy4();
                long endTime4= System.currentTimeMillis();
                System.out.println(endTime4-starTime4);//4复制完毕  用时32毫秒

        }
        public static void copy1() throws IOException{
                FileInputStream fis = new FileInputStream("E:\\Demo\\手心里的温柔.mp3");
                FileOutputStream fos = new FileOutputStream("E:\\Demo\\手心里的温柔1.mp3");
                int len = fis.read();
                while(len!=-1) {
                        fos.write(len);
                        len=fis.read();
                }
                System.out.println("1复制完毕");
                fos.close();
                fis.close();
        }
        public static void copy2() throws IOException{
                FileInputStream fis = new FileInputStream("E:\\Demo\\手心里的温柔.mp3");
                FileOutputStream fos = new FileOutputStream("E:\\Demo\\手心里的温柔2.mp3");
                byte[] arr = new byte[1024];
                int len = 0;
                while((len=fis.read(arr))!=-1){
                        fos.write(arr, 0, len);
                }
                System.out.println("2复制完毕");
                fos.close();
                fis.close();
        }
        public static void copy3() throws IOException{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\Demo\\手心里的温柔.mp3"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\Demo\\手心里的温柔3.mp3"));
                int len = 0;
                while((len=bis.read())!=-1){
                        bos.write(len);
                }
                System.out.println("3复制完毕");
                bos.close();
                bis.close();
        }
        public static void copy4() throws IOException{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\Demo\\手心里的温柔.mp3"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\Demo\\手心里的温柔4.mp3"));
                byte[] bytes = new byte[1024];
                int len = 0;
                while((len=bis.read(bytes))!=-1){
                        bos.write(bytes, 0, len);
                }
                System.out.println("4复制完毕");
                bos.close();
                bis.close();
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2