一、文件分割测试

这篇博客详细介绍了使用Java进行文件内容分割与合并的方法,通过testOne()和testTwo()函数操作1MB大小的数据块,展示了如何利用FileInputStream、FileOutputStream和BufferedIO流进行字节级别的文件处理。
摘要由CSDN通过智能技术生成

一、文件分割测试

  1. 一切皆为字节:

    • testOne() : 是分割方法。
    • testTwo() : 是合并方法。
    package name.yu.review;
    
    import org.junit.Test;
    
    import java.io.*;
    
    public class Test1 {
        @Test
        public void tesTone(){
            long starTime = System.currentTimeMillis();
            String fileName = "E:\\temp\\";
    
            OutputStream os = null;
            BufferedOutputStream  bos = null;
    
            OutputStream os2 = null;
            BufferedOutputStream  bos2 = null;
    
            InputStream is = null;
            BufferedInputStream bis = null;
    
            byte[] bytes = new byte[1024];
            int len = 0;
            long sum = 0;
            try {
                is = new FileInputStream(fileName + "video.mp4");
                bis = new BufferedInputStream(is);
    
                os = new FileOutputStream(fileName + "new1.mp4");
                bos = new BufferedOutputStream(os);
    
                os2 = new FileOutputStream(fileName + "new2.mp4");
                bos2 = new BufferedOutputStream(os2);
    
                while ((len = bis.read(bytes)) != -1){
    
                    if (sum <= 1024*1024*1024){
                        bos.write(bytes,0,len);
                    }else {
                        bos2.write(bytes,0,len);
                    }
                    sum = sum + len; // 一个 len = 1 kb
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (bis != null){
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bos != null){
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bos2 != null){
                    try {
                        bos2.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (os != null){
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (os2 != null){
                    try {
                        os2.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println(System.currentTimeMillis()-starTime);
    
        }
    
        @Test
        public void testTwo(){
            long startTime = System.currentTimeMillis();
            String fileName = "E:\\temp\\";
    
            InputStream is = null;
            BufferedInputStream bis = null;
    
            InputStream is2 = null;
            BufferedInputStream bis2 = null;
    
            OutputStream os = null;
            BufferedOutputStream  bos = null;
    
            int len = 0;
            byte[] bytes = new byte[1024];
    
            try {
                is = new FileInputStream(fileName + "new1.mp4");
                bis = new BufferedInputStream(is);
    
                os = new FileOutputStream(fileName + "new.mp4");
                bos = new BufferedOutputStream(os);
    
                while ((len = bis.read(bytes)) != -1){
                    os.write(bytes,0,len);
                }
                is2 = new FileInputStream(fileName + "new2.mp4");
                bis2 = new BufferedInputStream(is2);
                while ((len = bis2.read(bytes)) != -1){
                    os.write(bytes,0,len);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (bis != null){
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bis2 != null){
                    try {
                        bis2.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (is2 != null){
                    try {
                        is2.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bos != null){
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (os != null){
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println(System.currentTimeMillis()-startTime);
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值