重写文件拆分 l 完成文件合并

package Jobday13_作业;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        System.out.println("请输入要拆分的文件:");
        String s = new Scanner(System.in).nextLine();
        File file = new File(s);
        if (!file.isFile()) {
            System.out.println("输入的不是文件");
            return;
        }
        System.out.println("拆分文件大小(KB):");
        long size = 1024L * new Scanner(System.in).nextLong();
        try {
            chai(file, size);
            System.out.println("拆分成功");
        } catch (Exception e) {
            System.out.println("拆分失败");
            e.printStackTrace();
        }

        System.out.println("请输入要合并的目录:");
        String s1 = new Scanner(System.in).nextLine();
        File file1 = new File(s1);
        if (!file1.isDirectory()) {
            System.out.println("输入的不是目录");
            return;
        }
        System.out.println("请输入要核并到的文件名");
        String s3 = new Scanner(System.in).nextLine();
        File file2 = new File(s3);
//        if (!file2.isFile()) {
//            System.out.println("你输入保存合成的文件的文件名不是文件");
//            return;
//        }
        try {
            merger(file1, file2);
            System.out.println("合并成功");
        } catch (Exception e) {
            System.out.println("合并失败");
            e.printStackTrace();
        }
    }

    private static void merger(File file1, File file2) throws Exception {

        File[] files = file1.listFiles();
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                String s11 = o1.getName().substring(o1.getName().lastIndexOf('.') + 1, o1.getName().length());
                String s22 = o2.getName().substring(o2.getName().lastIndexOf('.') + 1, o2.getName().length());
                return Integer.parseInt(s11) - Integer.parseInt(s22);
            }
        });
        if (files == null) {
            return;
        }
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file2));
        for (File f : files) {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
                
            }
            in.close();
        }
out.close();
    }

    private static void chai(File file, long size) throws Exception {
        String name = file.getName();
        File dir = zhunBeiMulu(file);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream out = null;
        long byteCount = 0;
        int fileCount = 0;
        int b;
        while ((b = in.read()) != -1) {
            if (out == null || byteCount == size) {
                if (out != null) {
                    out.close();
                }
                out = new BufferedOutputStream(new FileOutputStream(new File(dir, name + "." + (++fileCount))));
                byteCount = 0;
            }
            out.write(b);
            byteCount++;
        }
        in.close();
        out.close();
    }

    private static File zhunBeiMulu(File file) {
        File dir = new File(file.getAbsolutePath() + "_spilt");
        if (!dir.exists()) {
            dir.mkdirs();
        } else {
            clear(dir);
        }
        return dir;
    }

    private static void clear(File dir) {
        File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        for (File f : files) {
            if (f.isFile()) {
                f.delete();
            } else {
                clear(f);// 清空f目录
                f.delete();// 删除目录

            }
        }

    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值