zoukankan      html  css  js  c++  java
  • 文件的分割与合并

    public class FileDivisionUniyer
    {
        /**
         * 文件分割合并器 将大文件分割成若干个小文件,将多个小文件合并成一个大文件
         */
     
        public static final String SUFFIX = ".pp";
     
        public static String[] divde(String fileName, long size) throws Exception
        {
            File file = new File(fileName);
            if (!file.exists() || !file.isFile())
                throw new Exception("指定文件不存在");
            File paretFile = file.getParentFile();
            long fileLength = file.length();// 大文件的大小
            int num = 0;// 被分割成小文件的数量
            if (size <= 0)// 除数不能为0
            {
                size = fileLength / 2;
            }
            if (fileLength % size == 0)
            {
                num = (int) (fileLength / size);
            } else
                num = (int) (fileLength / size + 1);
            String[] outFileName = new String[num + 5];
            long endFile = 0;
            int begainFile = 0;
            FileInputStream in = new FileInputStream(file);
            System.out.println(num);
            for (int i = 0; i < num; i++)
            {
                File file2 = new File(paretFile, file.getName() + i + SUFFIX);
                FileOutputStream out = new FileOutputStream(file2);// 如果文件不存在 在这里会新建一个文件 但是如果文件夹(目录)不存在会出错(不会新建)
                endFile += size;
                if (endFile > fileLength)
                    endFile = fileLength;
                for (; begainFile < endFile; begainFile++)
                    out.write(in.read());
                out.close();
                System.out.println(begainFile);
                outFileName[i] = file2.getAbsolutePath();
     
            }
            return outFileName;
        }
     
        public static void unit(String[] fileName, String newFileName) throws Exception
        {
            File file = new File(newFileName);
            FileOutputStream out = new FileOutputStream(file);//在这里如果目录存在而文件不想在会在此目录下新建一个文件,但是 目录不存在时会出错
            for (int i = 0; i < fileName.length; i++)
            {
                file = new File(fileName[i]);
                FileInputStream in = new FileInputStream(file);
                int c = 0;
                while ((c = in.read()) != -1)
                {
                    out.write(c);
                }
                in.close();
            }
            out.close();
     
        }
     
        public static void main(String[] args)
        {
            String fileNameString = "C:/temp/newTemp.pdf";
            try
            {
                String[] strings = divde(fileNameString, 0);
                unit(strings, "C:/hahaha1.pdf");
            } catch (Exception e)
            {
                // TODO: handle exception
                e.printStackTrace();
            }
     
        }
     
    }

     

    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    轻量级数据库sqlite的使用
    Integer引发的思考
    css限制显示行数
    数据库 chapter 17 数据仓库与联机分析处理技术
    数据库 chapter 15 对象关系数据库系统
    数据库 chapter 16 XML数据库
    数据库 chapter 14 分布式数据库系统
    数据库 chapter 11 并发控制
    数据库 chapter 12 数据库管理系统
    数据库 chapter 13 数据库技术新发展
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5710126.html
Copyright © 2011-2022 走看看