bmxm

递归

package filetrs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

/**
 * @program: springboot-demo
 * @description:
 * @Author: dongman
 * @create: 2021-05-29 11:46
 **/
public class FileIo {

    final static String targetPath = "D:\\Zfile\\targetPath\\"; //目标路径


    public static void main(String[] args) {
        copyDir("D:\\Download\\originPath");

    }

    public static void copyFile (String fileName){

        try {
            int bytesum = 0; //累计读取到的字节数
            int byteread = 0; //单次读取到的 字节数

            File oldfile = new File(fileName);
            if (oldfile.exists()) { //文件存在时
                InputStream inStream = new FileInputStream(fileName); //读入原文件
                FileOutputStream fs = new FileOutputStream(targetPath+ oldfile.getName());
                byte[] buffer = new byte[1444];
                while ( (byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                   // System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                fs.close();
                inStream.close();
                System.out.println("文件"+oldfile.getName()+"复制完毕!");
            }
        }
        catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();

        }
    }

    public static void copyDir(String sourcePath){  
        File file = new File(sourcePath);
        if (file.isDirectory()) {
            String[] fileList = file.list();
            for (String s : fileList) {
                String str = sourcePath+"\\"+s;
                //System.out.println(str);

                copyDir(str);

            }
        } else {
            if (sourcePath.endsWith(".pdf") || sourcePath.endsWith(".PDF")) {
               copyFile(sourcePath);
               // System.out.println(sourcePath);
            }
        }


    }
}

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-28
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
  • 2022-01-07
相关资源
相似解决方案