欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

IO流拷贝文件和目录

程序员文章站 2022-03-26 15:12:34
使用IO流来复制文件的练习这里关键是使用了一个递归的方法import java.io.*;public class CopyAll { public static void main(String[] args) { //拷贝源 File srcfile=new File("F:\\EV\\one"); //拷贝位置 File destfile=new File("F:\\EV\\two"); copy(srcf...

使用IO流来复制文件的练习

这里关键是使用了一个递归的方法

import java.io.*;

public class CopyAll {
    public static void main(String[] args) {
        //拷贝源
        File srcfile=new File("F:\\EV\\one");
        //拷贝位置
        File destfile=new File("F:\\EV\\two");
        copy(srcfile,destfile);
    }
    private static void copy(File srcfile,File destfile){
            if(srcfile.exists()){//判断路径是否存在
                if(srcfile.isFile()){//判断源文件是否是一个文件
                    //创建文件字节流(万能流)
                    FileInputStream fis=null;
                    FileOutputStream fos=null;
                    try {
                        fis=new FileInputStream(srcfile);
                        fos=new FileOutputStream(destfile.getAbsoluteFile()+"//"+srcfile.getName());
                        //每秒1mb的速度复制
                        byte[] bytes=new byte[1024*1024];
                        int readDate=0;
                        while ((readDate=fis.read(bytes))!=-1){
                            //边读边写
                            fos.write(bytes,0,readDate);
                        }
                        fos.flush();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        if(fis!=null){
                            try {
                                fis.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if(fos!=null){
                            try {
                                fos.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }return;
                }else{
                    //如果为目录的话获取目录下的所有子文件
                    File[] files=srcfile.listFiles();
                    for (File f:files) {
                        //遍历判断是否是子文件下是否有目录
                        if(f.isDirectory()){
                            //有目录的话就获取目录的路径
                            String desDir=destfile.getAbsolutePath()+"\\"+f.getName();
                            //这里创建一个新的目录文件夹
                            File newFile=new File(desDir);
                            if(!newFile.exists()){
                                newFile.mkdir();
                            }//然后使用递归获取下一个目录的文件
                            copy(f,newFile);
                        }else {//如果是文档的话递归直接复制出来
                            copy(f,destfile);
                        }
                    }
                }
            }else{
                System.out.println("路径错误!!!!");
            }
    }
}

本文地址:https://blog.csdn.net/w1256466374/article/details/112017334

相关标签: java