• 第四周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();
            }
            }
        }
      }
    }
    
    
  • 相关阅读:
    [BZOJ4631]踩气球
    [BZOJ1998][Hnoi2010]Fsk物品调度
    [BZOJ3624][Apio2008]免费道路
    [BZOJ1064][Noi2008]假面舞会
    [BZOJ4154][Ipsc2015]Generating Synergy
    [BZOJ1941][Sdoi2010]Hide and Seek
    [BZOJ2850]巧克力王国
    [BZOJ2683][BZOJ4066]简单题
    万年历(calendar)
    SACD ISO镜像中提取DSDIFF(DFF)、DSF文件
  • 原文地址:https://www.cnblogs.com/521xyh/p/5364871.html
Copyright © 2020-2023  润新知