• 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);
        }
    
    }
  • 相关阅读:
    HTML5(3) 拖放(Drag 和 Drop)
    HTML5(2) 画布Canvas
    HTML5(1) 介绍
    C#(99):System.IO.Path文件路径类
    SQL Server(00):逻辑函数
    SQL Server(00):删除表中的重复数据
    DevExpress03、XtraGrid(1)基本应用
    DevExpress11、TreeList
    DevExpress10、RichEditControl
    XtraEditors五、SpinEdit、TimeEdit
  • 原文地址:https://www.cnblogs.com/liangxiaojin/p/12464849.html
Copyright © 2020-2023  润新知