• 第四周JAVA学习笔记(四)


    使用BufferedInputStream和BufferedOutputStream来减少复制音频文件的时间,运行效果:
        
    
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import org.junit.Test;
    
    public class CopyFile {
        @Test
        public void testCopyFile(){
            long start = System.currentTimeMillis();
            String src="C:\Users\think\Desktop\Java作业4\a.mp3";
            String dest="C:\Users\think\Desktop\Java作业4\b.mp3";
            CopyFile(src,dest);
            long end = System.currentTimeMillis();
            System.out.print("花费的时间:" + (end - start));    
        }
        
        private void CopyFile(String src, String dest) {
        //使用缓冲流实现复制
        BufferedInputStream bis = null;
        BufferedInputStream bos = null;
        try {
            //1.提供读入写入文件
            File file1 = new File(src);
            File file2 = new File(dest);
            //2.创建相应的节点流
            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos =new FileOutputStream(file2);
            //3.将创建的节点流的对象传递给缓冲就的构造器中
            BufferedInputStream bis1 = new BufferedInputStream(fis);
            BufferedOutputStream bos1 = new BufferedOutputStream(fos);
            //4.具体的实现文件复制的操作
            byte[] b = new byte[1024]; //使用数组来实现传递,用1024个字节传递
            int len;
            while((len = bis1.read(b)) != -1){
                bos1.write(b,0,len);          
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //5.关闭流
            if(bos != null){
               try {
                bos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
            if(bis != null){
               try {
                bis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
        }
      }
    }
    
    
  • 相关阅读:
    读书笔记之理想设计的特征
    一些javascript 变量声明的 疑惑
    LINQ 使用方法
    Google MySQL tool releases
    读书笔记之设计的层次
    EF之数据库连接问题The specified named connection is either not found in the configuration, not intended to be used with the Ent
    转载 什么是闭包
    javascript面向对象起步
    Tips
    数据结构在游戏中的应用
  • 原文地址:https://www.cnblogs.com/521xyh/p/5364871.html
Copyright © 2020-2023  润新知