• 把大文件切割成小文件


    package com.jm.label.tools;
    /**
     * 把大文件切割成小文件
     */
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class FileSplit {
        
        
        public static void main(String[] args) {
            //调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小)
            cutFile("D:\FTPData\blacklist\2019-03-12-black_tbl_hf.txt", "D:\FTPData\blacklist\123",1024 * 1024 * 30);
        }
        
        /**
         *
         * @param src    需要切割的文件
         * @param endsrc    切割后文件的存放路径
         * @param num    每个文件的大小
         */
        public 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();
                }
            }
        }


    }

  • 相关阅读:
    深度学习GPU集群管理软件 OpenPAI 简介
    图片加数字盲水印
    Kubernetes核心概念简介
    一文详解 Linux 系统常用监控工具(top,htop,iotop,iftop)
    【OfficeWebViewer】在线预览Word,Excel~
    【Java】大文本字符串滤重的简单方案
    Java应用集群下的定时任务处理方案(mysql)
    《将博客搬至CSDN》
    [转]Fiddler抓取Android真机上的HTTPS包
    [Java Collection]List分组之简单应用.
  • 原文地址:https://www.cnblogs.com/java-h/p/10584002.html
Copyright © 2020-2023  润新知