zoukankan      html  css  js  c++  java
  • 课后作业3:编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件

    package wenjianyuliu;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    //编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件
    public class CutFile {
        public static void main(String[] args) {
            //调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小)
            cutFile("E:\新建文件夹\poem2.txt", "E:\80747",1024 * 1024 * 20);
        }
    
        private static void cutFile(String src, String endsrc, int num) {
            FileInputStream fis = null;
            File file = null;
            try {
                fis = new FileInputStream(src);
                file = new File(src);
                //创建规定大小的byte数组
                byte[] b = new byte[num];
                int len = 0;
                //name为以后的小文件命名做准备
                int name = 1;
                //遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中
                while ((len = fis.read(b)) != -1) {
                    //分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备
                    String name2 = file.getName();
                    int lastIndexOf = name2.lastIndexOf(".");
                    String substring = name2.substring(0, lastIndexOf);
                    String substring2 = name2.substring(lastIndexOf, name2.length());
                    FileOutputStream fos = new FileOutputStream(endsrc + "\\"+ substring + "-" + name + substring2);
                    //将byte数组写入对应的小文件中
                    fos.write(b, 0, len);
                    //结束资源
                    fos.close();
                    name++;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null) {
                        //结束资源
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

  • 相关阅读:
    vue学习简单入门
    Python3基础学习
    MySQL数据库索引详解
    使用nginx部署多个前端项目
    基于SpringBoot2.x和tkMapper快速搭建微服务项目脚手架工程
    安装篇-Linux安装maven3.5.2
    安装篇-安装maven3.6.1
    安装篇-安装Idea2019.3.3
    安装篇-jdk1.8安装
    【错误解决】Intellj(IDEA) warning no artifacts configured
  • 原文地址:https://www.cnblogs.com/zzstdruan1707-4/p/9979707.html
Copyright © 2011-2022 走看看