使用I/O流拷贝文件夹

使用I/O流拷贝文件夹


import java.io.*;

public class test{

    public static void main(String[] args) {
        test test = new test();
        String s = test.fileCopy("D:/", "E:/d");
        System.out.println(s);
    }
    
	public String fileCopy(String path,String pathcopy){

        File f1 = new File(path);
        File f2 = new File(pathcopy);

        if (!f2.isDirectory()){
            return "请输入需要存放的文件夹";
        }

        if (f1.isFile()){
            pathcopy = pathcopy+"/"+f1.getName();
            if (this.filecopyff(path,pathcopy)) {
                return "拷贝成功";
            }else {
                return "拷贝失败";
            }
        }else if (f1.isDirectory()){
            pathcopy = pathcopy+"/"+f1.getName();
            if (this.directoryCopy(path,pathcopy)) {
                return "拷贝成功";
            }else {
                return "拷贝失败";
            }
        }else {
            return "请输入文件或文件夹";
        }



    }

//    拷贝文件
    public boolean filecopyff(String path,String pathcopy){

        System.out.println(path+"==>"+pathcopy);
        FileInputStream fis = null;
        FileOutputStream fos = null;
//        DataInputStream dis1 =null;
//        DataOutputStream dos2 = null;
        try {
//            dis1 = new DataInputStream(new FileInputStream(path));
//            dos2 = new DataOutputStream(new FileOutputStream(pathcopy));
            fis = new FileInputStream(path);
            fos = new FileOutputStream(pathcopy);
            byte[] bytes = new byte[1024];
            int a;
            while ((a = fis.read(bytes)) != -1) {
                fos.write(bytes);
            }
            fos.flush();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
//    拷贝文件夹
    public boolean directoryCopy(String path,String pathcopy){
        File f1 = new File(path);
        File[] files = f1.listFiles();

        File f2 = new File(pathcopy);
        if (!f2.exists()){
            f2.mkdir();
        }
        String oldPath;
        String newPath;
        for (File file : files) {
            if (file.isFile()){
                oldPath = path+"/"+file.getName();
                newPath = pathcopy+"/"+file.getName();
                this.filecopyff(oldPath,newPath);
            }else {
                oldPath = path+"/"+file.getName();
                newPath = pathcopy+"/"+file.getName();
                this.directoryCopy(oldPath,newPath);
            }
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值