io流 文件的分割与合成

/**
     * 分割文件
     * @param src 被分割文件的地址
     * @param dir 文件被分割后的位置
     */
    public static void segmentation(String src, String dir) {
        try {
            File file = new File(dir);
            File f2 = new File(src);
            FileInputStream stream = new FileInputStream(src);
            int i = stream.available();//字节数
            String name = f2.getName();//文件名字
            int Bytes_per_file = i % 5 != 0 ? i / 5 + 1 : i / 5;//每个文件的字节数
            byte[] bytes = new byte[Bytes_per_file];
            int len = -1;
            int count = 0;
            if (file.isDirectory()) {
                while ((len = stream.read(bytes)) != -1) {//当文件的字节被读取完之后再次读取会变成-1
                    FileOutputStream stream1 =
                            new FileOutputStream(dir + "\\" + name.substring(0, name.indexOf(".")) + (count++) + ".bat");
                    stream1.write(bytes, 0, len);
                    stream1.close();//关闭流,关闭钱会强行清理缓冲区
                }
            } else {
                file.mkdirs();
                segmentation(src, dir);
            }
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
static String[] bats = new String[5];//被分割的文件数
/**
     * 获取被分割的文件并合成
     * @param s 被分割文件的地址
     * @param dir 要合成到哪里去
     * @return
     */
    public static String[] Flie(String s, String dir) {
        File file = new File(s);
        File[] files = file.listFiles();
        int i = 0;
        for (File f : files) {
            if (f.isDirectory()) {
                String name = f.getAbsolutePath();
                Flie(name, dir);
            } else if (f.isFile() && f.getName().endsWith(".bat")) {
                String path = f.getAbsolutePath();
                bats[i] = path;
                i++;
            }
        }
        FileSynthesis(bats, dir);//引入合成文件
        return bats;
    }
/**
     * 合成方法 耗时短
     * @param s  每一个被分割的文件
     * @param dir  创建合成文件的文件目录
     */
    public static void FileSynthesis(String[] s, String dir) {
        long a = System.currentTimeMillis();
        File file = new File(dir);
        file.mkdirs();
        file.delete();
        try {
            FileOutputStream ou = new FileOutputStream(dir, true);
            for (int i = 0; i < s.length; i++) {
                FileInputStream stream = new FileInputStream(s[i]);
                byte[] bytes = stream.readAllBytes();
                //System.out.println(Arrays.toString(bytes));
                ou.write(bytes);
            }
            long b = System.currentTimeMillis();
            System.out.println(b - a);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
      /**
     * 合成方法  费时间
     * @param s  每一个被分割的文件
     * @param dir  创建合成文件的文件目录
     */
    public static void FileSynthesis2(String[] s, String dir) {
        ArrayList<Byte> bytes1 = new ArrayList<>();
        File file = new File(dir);
        file.mkdirs();
        file.delete();
        try {
            for (int i = 0; i < s.length; i++) {
                FileInputStream stream = new FileInputStream(s[i]);
                byte[] bytes = stream.readAllBytes();
                for (byte aByte : bytes) {
                    bytes1.add(aByte);
                }
                System.out.println(Arrays.toString(bytes));
            }
            FileOutputStream stream = new FileOutputStream(dir);
            stream.write(bytes1.toString().getBytes(), 0, bytes1.size());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值