JAVA implements copying directories and files in directories

First of all, what we need to achieve is to copy a directory, its subdirectories, and the files contained in the directory to another directory. The files and their directories in the target directory must be consistent with the source directory.
We can sort out the ideas first. In the first step, we first create the source directory and the target directory, and then implement it by calling the copy method. The specific implementation method is implemented in the copy method.
In the copy, the source directory and the target directory need to be passed.
The specific ideas in the copy method are as follows:
First, we first obtain all sub-files in the source directory, and use the listFiles() method. After obtaining all sub-directories in the source directory, we use a FILE array to receive them, and then traverse them. When traversing a directory, call the copy method recursively, and return if it is a file. And when traversing, if the object is found to be a directory, the target directory needs to be created, and the target directory needs to be spliced ​​​​to generate.
The specific source code is as follows:
import java.io.*;

public class DirCopyTest { public static void main(String[] args) { File src=new File("D:\java notes\B station java study notes"); File des =new File("E:\"); copy (src, des); }




private static void copy(File src, File des) {

    //如果是文件那么递归结束copy调用,将文件进行复制操作
    if(src.isFile())
    {
        FileInputStream fis =null;//输入流
        FileOutputStream fos=null;//输出流
        try {
            fis=new FileInputStream(src);//获取源路径
            String path=des.getAbsolutePath()+src.getAbsolutePath().substring(3);//获取目标路径
            fos=new FileOutputStream(path);//将目标路径传入
            int k=0;
            //边读边写。
            byte [] b=new byte[1024*1024];
            while ((k= fis.read(b))!=-1)
            {
                fos.write(b,0,k);

            }

            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return;
    }
    File []file =src.listFiles();//获取src下的所有子目录
    for (File f :file)//对File【】数组进行遍历
    {
        if(f.isDirectory())
        {

            String srcc= f.getAbsolutePath();//源目录

            String dess=des.getAbsolutePath()+srcc.substring(3);//目标目录
            System.out.println(dess);
            File newFile=new File(dess);//将目标目录路径生成
            if(!newFile.exists())//如果目标目录路径不存在进行创建。
            {
                newFile.mkdirs();
            }
        }
        //System.out.println(f.getAbsolutePath());
        copy(f,des);//如果是目录进行递归带调用copy
    }
}

}

Supongo que te gusta

Origin blog.csdn.net/Kirihara_Yukiho/article/details/124795796
Recomendado
Clasificación