A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 18263368378 中级黑马   /  2016-4-28 22:47  /  651 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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();
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马