黑马程序员技术交流社区

标题: 字符流的问题 [打印本页]

作者: 小丑的眼泪    时间: 2015-4-24 19:16
标题: 字符流的问题
编写程序,采用多种方式,把d:\\video01.avi的内容复制到d:\\video02.avi
方式1:基本字节流一次读写一个字节
方式2:基本字节流一次读写一个字节数组
方式3:高效字节流一次读写一个字节
方式4:高效字节流一次读写一个字节数组


作者: 优质码农    时间: 2015-4-24 19:16
public class CopyVideo {
        public static void main(String[] args) throws IOException {
                long start = System.currentTimeMillis();
                //method1("D:\\video01.avi

", "D:\\video02.avi

");
                //method2("D:\\video01.avi

", "D:\\video02.avi

");
                //method3("D:\\video01.avi

", "D:\\video02.avi

");
                method4("D:\\video01.avi

", "D:\\video02.avi

");
                long end = System.currentTimeMillis();
                System.out.println("共耗时: "+ (end-start) +"毫秒");
        }

        //基本的流 一次一个字节
        public static void method1(String srcPath, String destPath) throws IOException {
                FileInputStream fis = new FileInputStream(srcPath);
                FileOutputStream fos = new FileOutputStream(destPath);
                //读取数据
                int by = 0;
                while ((by = fis.read()) != -1) {
                        //写数据到目的地
                        fos.write(by);
                }
                fis.close();
                fos.close();
        }

        //基本的流 一次一个字节数组
        public static void method2(String srcPath, String destPath) throws IOException {
                FileInputStream fis = new FileInputStream(srcPath);
                FileOutputStream fos = new FileOutputStream(destPath);
                //读取数据
                byte[] buffer = new byte[1024];
                int len  = 0;
                while( (len=fis.read(buffer)) != -1){
                        //写数据到目的地
                        fos.write(buffer, 0, len);
                }
                fos.close();
                fis.close();
        }

        //高效的流 一次一个字节
        public static void method3(String srcPath, String destPath) throws IOException {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcPath));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
                //读取数据
                int by = 0;
                while (( by=bis.read()) != -1) {
                        //写数据到目的地
                        bos.write(by);
                }
                bos.close();
                bis.close();
        }
       
        //高效的流 一次一个字节数组
        public static void method4(String srcPath, String destPath) throws IOException {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcPath));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
                //读取数据
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = bis.read(buffer)) != -1) {
                        //写数据到目的地
                        bos.write(buffer, 0, len);
                }
                bos.close();
                bis.close();
        }
}




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