Java之遍历文件夹下的文件,以及读取内容并Copy到另一文件夹下

话不多说直接上码。

package com.example.demo.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.util.LinkedList;

@EnableAutoConfiguration
@RestController
public class FormatConversionController {
    @RequestMapping("/stringDataView")
    public void stringDataView(){
     FormatConversionController.eachDir();//这是调用eachDir方法的,这个类命名名无视就好了。
    }
//以上几行是整理SpringBoot写的测试入口方法,这类、方法什么的在我本地太乱了,整了半个小时。。。各位就勉强略过那几行。
------------------------------
 **//文件夹遍历**
    public static void eachDir(){
     int fileNum = 0;//文件计数
     int folderNum = 0;//初始为0
     File file = new File("F:\\carDataBase");
     if (file.exists()) {
     LinkedList list = new LinkedList();
     File[] files = file.listFiles();
     for (File file2 : files) {
     if (file2.isDirectory()) {
       System.out.println("文件夹:" + file2.getAbsolutePath());
       list.add(file2);
       folderNum++;
     } else {
       System.out.println("文件:" + file2.getPath());
       String copyFileName = "copy_"+file2.getName();//新文件名
       String isok =  FormatConversionController.readerFile(file2.getAbsolutePath(),copyFileName);
    if ("ok".equals(isok)){
       try {
       Thread.sleep(2000);
       System.out.println("文件1:" + file2.getAbsolutePath()+"拷贝成功!");
       } catch (InterruptedException e) {
       e.printStackTrace();
       }
    }else{
       try {
            System.out.println("文件1:" + file2.getAbsolutePath()+"拷贝异常!");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
       System.out.println("文件1:" + file2.getAbsolutePath());
       fileNum++;
      }
   }
     File temp_file;
     while (!list.isEmpty()) {
         temp_file = list.removeFirst();
         files = temp_file.listFiles();
         for (File file2 : files) {
             if (file2.isDirectory()) {
             System.out.println("文件夹:" + file2.getAbsolutePath());
             list.add(file2);
             folderNum++;
             } else {
             System.out.println("文件2:" + file2.getAbsolutePath());
             fileNum++;
             }
        }
  }
} else {
  System.out.println("文件不存在!");
}
  System.out.println("文件夹共有:" + folderNum + ",文件共有:" + fileNum);
}

**//Copy文件的**
    public static String readerFile(String yFileName,String nFileName){
        InputStream input = null;
        OutputStream output = null;
    try {
          yFileName = yFileName.replaceAll("/\\/","\\\\");
          input = new FileInputStream(yFileName);
          output = new FileOutputStream("F:\\CopyCarDataBase\\"+nFileName,true);
          byte[] buf = new byte[1024*1024];
          int bytesRead;
          while ((bytesRead = input.read(buf)) > 0) {
            output.write(buf, 0, bytesRead);
          }

    } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        yFileName = null;
        nFileName = null;
        return "ok";
   }

 }**//这个括号是SpringBoot类的括号**

你可能感兴趣的:(java,文件遍历,文件夹操作,拷贝文件)