java 文件夹及其子目录 子文件的复制

package test815.night;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class FileTest {
private static boolean flag = false;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String path = null;// "D:\\t";//要复制的文件夹
    String path2 = null;// "D:\\tt";
    String info = "请输入您要复制的文件的路径: 如【D:\\t  如果不输入盘符  ,则默认当前目录】";
    path = inputPath(info);
    String info2 = "请输入您要粘贴的文件的路径: 如【D:\\tt  如果不输入盘符  ,则默认当前目录】";
    path2 = inputPath(info2);
    System.out.println("开始复制,请稍等……");

    isFile(path, path2);
    if (flag) {

        System.out.println("复制完毕end");
    } else {

        System.out.println("复制失败end");
    }

}

public static String inputPath(String info) {
    System.out.println(info);
    Scanner sc = new Scanner(System.in);
    String pathString = sc.nextLine();
    return pathString;
}

// 对原路径进行判断
public static void isFile(String path, String path2) {

    File file = new File(path);// 获取path地址对应的文件
    if (file.exists()) {
        File newpathFile = new File(path2);// 建立新的路径文件
        newpathFile.mkdir();
        String[] str = file.list();// 获得该文件下的子文件和子目录的名称
        // System.out.println(str.length);

        System.out.println(path + "    中原有文件为: " + Arrays.toString(str));
        for (String string : str) {// 遍历
            String oldpath = path + "\\" + string;// 组合新的路径
            String newpath = path2 + "\\" + string;// 对应的新文件夹 组合路径
            File oldFile = new File(oldpath);// 取得对应的文件
            if (oldFile.isDirectory()) {// 判断是否为目录
                File newFile = new File(newpath);// 在对应的新路径下建立文件夹
                newFile.mkdir();
                isFile(oldpath, newpath);// 递归 进入到子文件夹内
            } else {
                copy(oldpath, newpath);// 如果是文档,则开始复制
            }
        }

    } else {

        System.out.println(path + "   原始路径有误!");
    }

}

public static void copy(String path, String path2) {
    flag = false;
    FileInputStream fis = null;
    FileOutputStream fos = null;
    byte[] bs = new byte[2048];
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        fis = new FileInputStream(path);
        fos = new FileOutputStream(path2);
        bis = new BufferedInputStream(fis);
        bos = new BufferedOutputStream(fos);
        int x = bis.read(bs);
        while (x != -1) {
            bos.write(bs, 0, x);
            x = bis.read(bs);
        }
        bos.flush();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        flag = true;
    }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值