zoukankan      html  css  js  c++  java
  • java 复制某一文件夹下的所有文件到另一个文件夹下

    场景:复制某一文件夹下的所有文件到另一个文件夹下

    代码:

    package cn.lj.java;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    
    public class FlowLogUpLoad {
    
        //文件夹的拷贝
        public static void copyDir(String sourcePathDir, String newPathDir) {
            File start = new File(sourcePathDir);
            File end = new File(newPathDir);
            String[] filePath = start.list();//获取该文件夹下的所有文件以及目录的名字
            if(!end.exists()) {
                end.mkdir();
            }
            boolean flag = false;
            for(String temp : filePath) {
                //添加满足情况的条件
                if(new File(sourcePathDir + File.separator + temp ).isFile() && temp.endsWith(".dat")) {
                    //为文件则进行拷贝
                    flag = copyFile(sourcePathDir + File.separator + temp, newPathDir + File.separator+temp );
                }
                if(flag){
                    System.out.println("文件:" + temp + ",复制成功!");
                }else{
                    System.out.println("文件:" + temp + ",复制失败!");
                }
            }
        }
    
        //文件的拷贝
        public static boolean copyFile(String sourcePath, String newPath) {
            boolean flag = false;
            File readfile = new File(sourcePath);
            File newFile = new File(newPath);
            BufferedWriter bufferedWriter = null;
            Writer writer = null;
            FileOutputStream fileOutputStream = null;
            BufferedReader bufferedReader = null;
            try{
                fileOutputStream = new FileOutputStream(newFile, true);
                writer = new OutputStreamWriter(fileOutputStream,"UTF-8");
                bufferedWriter = new BufferedWriter(writer);
                
                bufferedReader = new BufferedReader(new FileReader(readfile));
                
                String line = null;
                while((line = bufferedReader.readLine()) != null){
                    bufferedWriter.write(line);
                    bufferedWriter.newLine();
                    bufferedWriter.flush();
                }
                flag = true;    
            } catch(IOException e) {
                flag = false;
                e.printStackTrace();
            } finally {
                try {
                    if(bufferedWriter != null){
                        bufferedWriter.close();
                    }
                    if(bufferedReader != null){
                        bufferedReader.close();
                    }
                    if(writer != null){
                        writer.close();
                    }
                    if(fileOutputStream != null){
                        fileOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return flag;
        }
        
        public static void main(String[] args) {
            
            String sourcePath = "D:\home\before\20200120";
            String newPath = "E:\home\after\20200120";
            System.out.print("From:" + sourcePath);
            System.out.print("To:" + newPath);
            copyDir(sourcePath, newPath);
        }
    
    }
  • 相关阅读:
    和至少为 K 的最短子数组
    使用VS code编写C++无法实时检测代码的解决办法
    anaconda安装VSCODE后,python报错
    神经网络中sigmod函数和tanh函数的区别
    获取本机IP
    windows C++捕获CMD命令输出
    windows下面生成 dump
    windows 控制台命令输出 捕获
    不使用PEM 文件建立SSL通道
    OpenSSL socket 服务端
  • 原文地址:https://www.cnblogs.com/liangxiaojin/p/12464849.html
Copyright © 2011-2022 走看看