FileUtils.java 3.7 KB
package com.vion.utils;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author 谢明辉
 * @createDate 2018-10-30
 * @description
 */

public class FileUtils {
    public static Pattern pattern = Pattern.compile("[0-9]{8}");

    public static void getFiles(List<File> fileList, String basePath, Integer begin, Integer end) {
        File file = new File(basePath);
        if (!file.exists()) {
            System.out.println(basePath + " 不存在");
            return;
        }
        File[] files = file.listFiles();
        if (files != null) {
            for (File file1 : files) {
                if (file1.isDirectory()) {
                    if (file1.getName().matches("[0-9]{8}")) {
                        if (Integer.parseInt(file1.getName()) >= begin && Integer.parseInt(file1.getName()) <= end) {
                            getFiles(fileList, getAvailablePath(file1), begin, end);
                        }
                    } else {
                        getFiles(fileList, getAvailablePath(file1), begin, end);
                    }
                } else {
                    if (file1.getName().contains("face-0") || file1.getName().contains("body-0")) {
                        fileList.add(file1);
                    }
                }
            }
        }
    }

    private static String getAvailablePath(File file) {
        return file.getPath().replace("//", "/");
    }

    public static String generator(String name, String pathPrefix, String pathPostfix, String channelSerialNum) {
        StringBuilder result = new StringBuilder();
        if (pathPrefix != null) {
            result.append(pathPrefix);
        }
        if (pathPostfix != null) {
            if (name.endsWith(pathPostfix)) {
                name = name.substring(0, name.lastIndexOf(pathPostfix));
            }
        }

        if (name.contains("face")) {
            result.append("face").append("/");
        } else if (name.contains("body")) {
            result.append("body").append("/");
        }

        if (name.length() > 25) {
            String dateStr = name.substring(name.length() - 25, name.length() - 17);
            result.append(dateStr).append("/");
        }

        result.append(channelSerialNum).append("/").append(name);
        if (pathPostfix != null) {
            result.append(pathPostfix);
        }
        return result.toString();
    }

    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        //余数
        int remainder = source.size() % n;
        //商
        int number = source.size() / n;
        //偏移量
        int offset = 0;
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remainder > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }


    public static boolean isNotNull(String s) {
        return !(s == null || "".equals(s));
    }


    public static String getPathName(String basePath, Integer dateNumber, String channelSerialNum, String fileName, boolean isFeature) {
        StringBuilder sb = new StringBuilder();
        sb.append(basePath).append(dateNumber).append("\\");
        if (isNotNull(channelSerialNum)) {
            sb.append(channelSerialNum).append("\\");
        }
        sb.append(fileName);
        if (isFeature) {
            sb.append(".feature");
        }
        return sb.toString();
    }
}