java File实用工具

File实用工具(一)
新人第一次写,从工具类开始
1、copy文件夹下所有文件
2、按文件名称或后缀选择性copy文件夹下的不同文件
3、文件夹下同类型文件合并
4、文件分割

package util;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

/**
 * Author:varCode
 * Date:2019-02-15 10:29
 * Description:<描述>
 */
public class FileUtil {
    /**
     * 拷贝文件或文件夹
     * @param fromDir 源文件路径
     * @param toDir   拷贝至的路径
     * @param filter  选择性拷贝(后缀名、文件名)
     */
    public static void copy(String fromDir, String toDir, String filter) {

        if (new File(fromDir).isDirectory()) {
            List<File> files = getFiles(fromDir);
            for (File file : files) {
                if (null != filter && !(file.getName().contains(filter))) {
                    continue;
                }
                try {
                    FileInputStream fis = new FileInputStream(file);
                    File copyFile = new File((file.getAbsolutePath()).replace(fromDir, toDir));
                    if(!copyFile.getParentFile().exists()){
                        copyFile.getParentFile().mkdirs();
                    }
                    FileOutputStream fos = new FileOutputStream(copyFile);
                    byte[] b = new byte[fis.available()];
                    fis.read(b);
                    fos.write(b);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            try {
                FileInputStream fis = new FileInputStream(fromDir);
                FileOutputStream fos = new FileOutputStream(toDir);
                byte[] b = new byte[fis.available()];
                fis.read(b);
                fos.write(b);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 拷贝文件或文件夹
     * @param fromDir
     * @param toDir
     */
    public static void copy(String fromDir, String toDir) {
        copy(fromDir, toDir, null);
    }

    /**
     * 文件夹下相同的文件类型整合成一个文件(文本、字符类)
     * @param path     文件夹路径
     * @param location 合并文件名存放路径
     */
    public static void fileUnion(String path, String location) {
        List<File> files = getFiles(path);
        HashSet<String> types = new HashSet<>();
        for (File file : files) {
            types.add(suffix(file));
        }
        try {
            for (String type : types) {
                File newFile = new File(location);
                if(!newFile.exists()){
                    newFile.mkdirs();
                }
                FileOutputStream fos = new FileOutputStream(new File(location + File.separator + UUID.randomUUID() + type));
                for (File file : files) {
                    if (file.getName().endsWith(type)) {
                        FileInputStream fis = new FileInputStream(file);
                        byte[] b = new byte[fis.available()];
                        fis.read(b);
                        fos.write(b);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件按大小分割(文本、字符类)
     * @param path 源文件路径
     * @param size 分割大小(M)
     */
    public static void splitFile(String path, double size){
        File file = new File(path);
        try {
            FileInputStream fis = new FileInputStream(file);
            double total = (double)(fis.available()) / (1024 * 1024);
            int num = total % size == 0 ? (int) (total / size) : (int) (total / size) + 1;
            for (int i = 0; i < num; i++) {
                File splitFile = new File(file.getAbsolutePath().replace(suffix(file),"") + "(" + (i+1) + ")" + suffix(file));
                FileOutputStream fos = new FileOutputStream(splitFile);
                if(i == num-1){
                    byte[] b = new byte[fis.available()];
                    fis.read(b);
                    fos.write(b);
                }else{
                    byte[] b = new byte[(int)(size*1024*1024)];
                    fis.read(b);
                    fos.write(b);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取目录下所有的文件
     * @param path 目录
     * @return 所有文件
     */
    public static List<File> getFiles(String path) {
        ArrayList<File> fileList = new ArrayList<>();
        File file = new File(path);
        File[] files = file.listFiles();
        for (File childFile : files) {
            if (childFile.isFile()) {
                fileList.add(childFile);
            } else {
                fileList.addAll(getFiles(childFile.getAbsolutePath()));
            }
        }
        return fileList;
    }

    /**
     * 获取文件后缀(.png\.txt)
     * @param file
     * @return
     */
    public static String suffix(File file){
        return file.getName().substring(file.getName().lastIndexOf("."));
    }

    /**
     * 关闭流资源
     */
    public static void closeIO(Object... objs) throws IOException {
        for (Object obj :
                objs) {
            if (obj instanceof InputStream) {
                ((InputStream) obj).close();
            } else if (obj instanceof OutputStream) {
                ((OutputStream) obj).close();
            } else throw new IOException();
        }
    }
}

测试代码

public class Test {
    public static void main(String[] args){
        //测试 copy
        //FileUtil.copy("C:\\Users\\28\\Desktop\\笔记","C:\\Users\\28\\Desktop\\note");
        //测试 copy filter
        //FileUtil.copy("C:\\Users\\28\\Desktop\\笔记","C:\\Users\\28\\Desktop\\note","pdf");
        //测试 fileUnion
        //FileUtil.fileUnion("C:\\Users\\28\\Desktop\\dir","C:\\Users\\28\\Desktop\\dir");
        //测试 splitFile
        //FileUtil.splitFile("C:\\Users\\28\\Desktop\\Java面试问题集.pdf",1.0);
    }
}
发布了8 篇原创文章 · 获赞 6 · 访问量 2607

猜你喜欢

转载自blog.csdn.net/linhao_obj/article/details/87474744