zoukankan      html  css  js  c++  java
  • 把大文件切割成小文件

    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();
                }
            }
        }


    }

  • 相关阅读:
    【工具篇】抓包中的王牌工具—Fiddler (2-工具介绍)
    SQL注入攻击的常见方式及测试方法
    Xshell访问和连接Linux
    【户口篇】换房过程中,户口怎么迁移?
    【计算机篇】二维码解析、短地址生成器
    【工具篇】Sublime Text 2/3 安装汉化破解、插件包安装教程详解
    【通用】登录功能测试用例
    【工具篇】接口测试神器 -- Postman 入门教程
    【生活篇】微信运动刷步,高达98000!微信运动计步作弊教程!
    关于The C compiler "arm-none-eabi-gcc" is not able to compile a simple test program. 的错误自省...
  • 原文地址:https://www.cnblogs.com/java-h/p/10584002.html
Copyright © 2011-2022 走看看