Java里File类操作,递归遍历文件夹,复制文件夹,文件分块后再合并文件

在Java面试中File的笔试题很多。出镜率比较高的是递归遍历文件夹。其次在有的项目里,文件过大,需要进行文件分块后再进行合并。也写了一个简单的操作。遍历文件夹顺便也取出文件夹里最大文件以及最小文件。记录一下。码上
package com.macro.mall.demo.test.io;

import java.io.*;

/**
 * @author zxw
 * @version 1.0
 * @description 文件操作
 * @data: 2020/2/20 18:10
 */
public class FileTest {

    private static long minLength = Integer.MAX_VALUE;
    private static long maxLength = 0;
    private static File minFile;
    private static File maxFile;

    public static void main(String[] args) {
        File file = new File("e:\\else");
        fileTraverse(file);
        System.out.println("最大文件:" + maxFile.getName() + "大小为:" + maxLength);
        System.out.println("最小文件:" + minFile.getName() + "大小为:" + minLength);
        File splitFile = new File("d:\\pic\\basic\\split.png");
        splitFile(splitFile);
        File mergeFile = new File("d:\\pic\\test\\merge.png");
        mergeFile(mergeFile);
        File srcFile = new File("e:\\aa");
        File targetFile = new File("e:\\bb");
        folderCopy(srcFile, targetFile);
    }

    /**
     * 遍历文件夹,获得最大文件,最小文件
     *
     * @param file 需要遍历的文件
     */
    private static void fileTraverse(File file) {
        //获得文件数组遍历
        File[] files = file.listFiles();
        for (File childFile : files) {
            if (childFile.isFile()) {
                //如果是文件进行文件操作
                if (childFile.length() < minLength && childFile.length() > 0) {
                    //进行赋值
                    minLength = childFile.length();
                    minFile = childFile;
                } else if (childFile.length() > maxLength) {
                    maxLength = childFile.length();
                    maxFile = childFile;
                }
            } else {
                //不是文件进行递归
                fileTraverse(childFile);
            }
        }
    }

    /**
     * 复制文件夹
     *
     * @param srcFolder    需要复制的文件夹
     * @param targetFolder 目标文件夹
     */
    private static void folderCopy(File srcFolder, File targetFolder) {
        try {
            //判断目标文件夹是否存在
            if (!targetFolder.exists()) {
                targetFolder.mkdir();
            }
            //获得源文件夹所有子文件
            File[] srcFiles = srcFolder.listFiles();
            for (File srcFile : srcFiles) {
                //创建复制目标文件
                File targetFile = new File(targetFolder.getAbsoluteFile() + "/" + srcFile.getName());
                if (!srcFile.isFile()) {
                    //源文件为文件夹,目标文件创建文件夹
                    targetFile.mkdir();
                    //递归
                    folderCopy(srcFile, targetFile);
                } else {
                    //不是文件夹,复制该文件(此处可以抽取为赋值文件方法)
                    FileInputStream fis = new FileInputStream(srcFile);
                    FileOutputStream fos = new FileOutputStream(targetFile);
                    byte[] bytes = new byte[1024 * 8];
                    int len = fis.read(bytes);
                    while (len != -1) {
                        fos.write(bytes, 0, len);
                        len = fis.read(bytes);
                    }
                    fis.close();
                    fos.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 分割文件
     *
     * @param file 需要分割的文件
     */
    private static void splitFile(File file) {
        //创建文件输入流,输出流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int len, index = 0;
            while ((len = fis.read(bytes)) != -1) {
                fos = new FileOutputStream("d:\\pic\\test\\merge-" + (index++) + ".png");
                fos.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                assert fis != null;
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert fos != null;
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 合并文件
     *
     * @param file 合并后的文件
     */
    private static void mergeFile(File file) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        int index = 0;
        try {
            //只输出一个文件,一个输出流集合
            fos = new FileOutputStream(file);
            //因为不确定分块文件有多少,所以需要死循环
            while (true) {
                //通过命名规则获得分块文件  先获得去除后缀的名字
                String name = file.getName().substring(0, file.getName().lastIndexOf("."));
                File childFile = new File(file.getParent(), name + "-" + (index++) + ".png");
                //如果子文件没有了,代表合并完毕
                if (!childFile.exists()) {
                    System.out.println("合并完毕!");
                    break;
                }
                //将获得子文件转换成输出流
                fis = new FileInputStream(childFile);
                //创建数组,长度为文件的大小
                byte[] bytes = new byte[(int) childFile.length()];
                fis.read(bytes);
                fis.close();
                //写出文件,保证输出流为同一对象
                fos.write(bytes);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                assert fos != null;
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值