JAVA-IO 指定目录中查找文件,文件合并分割

指定目录中查找文件

public  static List<String> findFile(File target,String fileName){
    ArrayList<String> path = new ArrayList<>();
    if(target==null) {
        return path;
    }
    if(target.isDirectory()){
        File[] files = target.listFiles();
        if(files!=null){
            for(File f: files){
                findFile(f,fileName);
            }
        }
    }else{
        String name = target.getName().toLowerCase();
        if(name.contains(fileName)){
           path.add(target.getAbsolutePath());
        }
    }
    return path;
}

文件合并分割 

/**
 * 文件分割
 * @param targetFile 待分割文件
 * @param cutSize 每个文件大小
 */
public static void division(File targetFile, long cutSize,String targetPath) {

    if (targetFile == null) {
        return;
    }
    // 计算总分割的文件数
    int num = targetFile.length() % cutSize == 0 ? (int) (targetFile.length() / cutSize):(int) (targetFile.length() / cutSize + 1);
    try {
        // 构造一个文件输入流
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(targetFile));
        BufferedOutputStream out = null;

        byte[] bytes = null;
        int len = -1;
        int count = 0;

        //循环次为生成文件的个数
        for (int i = 0; i < num; i++) {

            out = new BufferedOutputStream(
                    new FileOutputStream(new File(targetPath+ (i + 1) + "-temp-" + targetFile.getName())));


            if (cutSize <= 1024) {
                bytes = new byte[(int) cutSize];
                count = 1;
            } else {
                bytes = new byte[1024];
                count = (int) cutSize / 1024;
            }
            //count放在前面避免多度一次
            while (count > 0 && (len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
                out.flush();
                count--;
            }

            //计算每个文件大小除于1024的余数,决定是否要再读取一次
            if (cutSize % 1024 != 0) {

                bytes = new byte[(int) cutSize % 1024];
                len = in.read(bytes);
                out.write(bytes, 0, len);
                out.flush();
                out.close();
            }
        }
        in.close();

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

/**
 * 合并文件
 * @param es 待合并的文件
 * @param targetFile 目标文件
 */
public  static void merge(Enumeration<InputStream> es, String targetFile) {
    SequenceInputStream sis = new SequenceInputStream(es);
    try {
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(targetFile));

        byte[] bytes = new byte[1024];
        int len = -1;
        while((len=sis.read(bytes))!=-1){
            bos.write(bytes,0,len);
            bos.flush();
        }
        bos.close();
        sis.close();
        System.out.println("合并完成.");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值