没事写个工具类玩

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
package com.asm.demo;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author CYH
 */
public class Tools {

    private static ThreadLocal<List<Path>> local = ThreadLocal.withInitial(LinkedList::new);

    /**
     * 文件分割
     * @param path
     * @param num
     * @return
     * @throws IOException
     */
    public static File[] splitFiles(String path,int num) throws IOException {
        if (num <= 0) {
            throw new RuntimeException("文件分割数必须大于等于1");
        }

        File file = new File(path);
        if (!file.exists()) {
           throw new FileNotFoundException("待分割的文件不存在");
        }

        RandomAccessFile raf = new RandomAccessFile(file,"r");
        long fileSize = file.length() / 4;

        String collect = Arrays.stream(file.getParent().split("\\\\")).collect(Collectors.joining("/"));
        BufferedOutputStream bfo = null;
        for (int i = 0; i < num; i++) {

            bfo = new BufferedOutputStream(
                    new FileOutputStream(
                            new File(collect+"/"+i+file.getName()),true));
            raf.seek(fileSize*i);
            byte[] bt = new byte[1024];
            //单次读取
            int len;
            //已读取数
            int fileSizeSum = 0;
            //分割文件需读取数
            long fileSizeSumAll;
            if (i == (num-1)) {
                fileSizeSumAll = file.length()-(num-1)*fileSize;
            } else {
                fileSizeSumAll = fileSize;
            }

            while ( (len = raf.read(bt)) != -1){

                fileSizeSum += len;
                if (fileSizeSum > fileSizeSumAll) {
                    //如果读取的字节数大于文件分割大小直接创建文件以文件大小结尾
                    bfo.write(bt,0, (int) (len - (fileSizeSum-fileSizeSumAll)));
                    break;
                }else{
                    bfo.write(bt,0,len);
                }

                //如果读取的字节数小于文件分割大小则输出
            }
            bfo.flush();
            bfo.close();
        }


        return null;
    }

    public static void main(String[] args) {
        System.out.println(iteratorFile("D:\\work\\asm-demo",".class"));
    }
    /**
     *包扫描
     * @param path
     * @param suffs
     * @return
     */
    public static Set<String> iteratorFile(String path,String... suffs) {
        Set<String> pack = new HashSet<>();
        try {
            Path path1 = Paths.get(path);

            Files.walkFileTree(path1, new IterFileVisitor(suffs));
            List<Path> paths = local.get();

            for (Path item : paths) {

                StringBuffer sbf = new StringBuffer();

                for (int i = path1.getNameCount(); i < item.getNameCount(); i++) {
                    sbf.append(item.getName(i));
                    sbf.append(".");
                }
                String s =sbf.toString();
                for (int i = 0; i < suffs.length; i++) {
                    s = s.replaceAll(String.format("%s.$", suffs[i]), "");
                }
                pack.add(s);
            }

            local.remove();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return pack;
    }

    /**
     *
     */
    private static class IterFileVisitor extends SimpleFileVisitor<Path>{

        private String[] suffixs;

        public IterFileVisitor(String[] suffixs) {
            this.suffixs = suffixs;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

            boolean b = Arrays.stream(suffixs).noneMatch(x -> file.toString().endsWith(x));
            if (suffixs != null && b) {
               return super.visitFile(file, attrs);
            }
            List<Path> paths = local.get();
            paths.add(file);
            return super.visitFile(file, attrs);
        }
    }
}

 

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看