使用Java将当前目录下的内容复制到另一个目录下

问题描述

将《作业原始》文件夹中的内容(仅包括文本文件和目录)复制到《作业目标》文件夹中,文件内容也要使用流进行完整复制。

代码

//树结点
public class Node {
    
    
    private Node left;
    private Node right;
    private String name;
    private String path;
    private int flag;//0文件夹,1文件

    public int getFlag() {
    
    
        return flag;
    }

    public void setFlag(int flag) {
    
    
        this.flag = flag;
    }

    public String getPath() {
    
    
        return path;
    }

    public void setPath(String path) {
    
    
        this.path = path;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public Node(String name,String path,int flag){
    
    
        this.name=name;
        this.path=path;
        this.flag=flag;
    }


    public Node getLeft() {
    
    
        return left;
    }

    public void setLeft(Node left) {
    
    
        this.left = left;
    }

    public Node getRight() {
    
    
        return right;
    }

    public void setRight(Node right) {
    
    
        this.right = right;
    }

    @Override
    public String toString(){
    
    
        return this.name+" "+this.getPath()+" ";
    }

}

//主程序
public class demo{
    
    

    public static void main(String[] args) {
    
    
        //原文件夹目录
        String rootPath="C:\\Users\\xxx\\Desktop\\作业\\作业原始";
        //目标文件夹目录
        String aimPath="C:\\Users\\xxx\\Desktop\\作业\\作业目标";
        //创建根结点
        Node root=new Node("作业原始",rootPath,0);
        //生成树
        getFiles(root);
        //更改根结点目录 为 目标目录
        root.setPath(aimPath);
        //在目标目录下创建目录
        preOrderTraverse1(root);
        //写内容

    }

    //生成树 左结点文件夹,右结点文件
    public static void getFiles(Node root){
    
    
        File file = new File(root.getPath());
        //工作指针
        Node temp=root;
        //递归获取所有文件
        File[] sub=file.listFiles();
        for(File f:sub){
    
    
            if(f.getName().contains(".xlsx")){
    
    
                continue;
            }
            if(f.isDirectory()){
    
    
                Node n=new Node(f.getName(),f.getAbsolutePath(),0);
                //挂左结点
                while(temp.getLeft()!=null) {
    
    
                    temp=temp.getLeft();
                }
                temp.setLeft(n);
                getFiles(n);
            }else{
    
    
                Node n=new Node(f.getName(),f.getAbsolutePath(),1);
                //挂右结点
                while(temp.getRight()!=null) {
    
    
                    temp=temp.getRight();
                }
                temp.setRight(n);
            }
        }
        return;
    }

    //遍历结点
    public static void preOrderTraverse1(Node root) {
    
    
        if (root != null) {
    
    
            //从当前目录的文件中读取内容
            String content="";
            if(root.getFlag()==1){
    
    
                content=readFileContent(root);
            }
            //获取目标文件目录
            File f=new File(root.getPath().replace("作业原始","作业目标"));
            if(root.getFlag()==0&&!f.exists()){
    
    
              f.mkdir();
            }
            else if(root.getFlag()==1&&!f.exists()){
    
    
                try {
    
    
                    //f.createNewFile();
                    BufferedWriter bw=new BufferedWriter(new FileWriter(f.getPath()));
                    bw.write(content);
                    bw.close();

                }catch (Exception e){
    
    
                    e.printStackTrace();
                }
            }
            preOrderTraverse1(root.getLeft());
            preOrderTraverse1(root.getRight());
        }
    }

    //读文件
    public static String readFileContent(Node node) {
    
    
        File file = new File(node.getPath());
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
    
    
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
    
    
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (reader != null) {
    
    
                try {
    
    
                    reader.close();
                } catch (IOException e1) {
    
    
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42451178/article/details/104786340