码迷,mamicode.com
首页 > 编程语言 > 详细

javaIo——运用字节流完整复制文件夹及文件

时间:2016-05-11 13:37:53      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

     突发奇想要写一段全部复制文件夹的代码,可以复制一个文件夹到任何目录(包括盘符根目录或者文件夹根目录),感觉自己写的很冗余,新手希望大牛指点一下!

public class Test {
    int a = 0;
    static File startPath = null;
    static File endPath = null;
    public static void main(String[] args) throws IOException {
    Scanner input = new Scanner(System.in);
    System.out.println("请输入您要复制的原目录:(完整目录)");
    startPath = new File(input.next());
    System.out.println("请输入您要复制的目标目录:(完整目录)");
    endPath = new File(input.next());
    Test2016510 test2016510 = new Test2016510();
    test2016510.copy(startPath, endPath);
    System.out.println("复制完成");
}
@SuppressWarnings("unused")
public void copy(File startFile, File endFile) throws IOException {

    File startFilePath = startFile;
    File endFilePath = endFile;

    File[] startfilePaths = startFilePath.listFiles();
    if (startFile.exists()) {
        for (File file : startfilePaths) {

            if (file.isDirectory()) {
                if (file != null) {
                    String ss = file.toString().substring(3,
                            file.toString().length());
                    File newFile = new File(endFilePath.toString() + "\\"
                            + ss);
                    newFile.mkdirs();
                    this.copy(file, newFile);
                } else {
                    String ss = file.toString().substring(3,
                            file.toString().length());
                    File newFile = new File(endFilePath.toString() + "\\"
                            + ss);
                    newFile.mkdirs();
                }
            } else if (file.isFile()) {
                String ss = file.getPath().substring(3,
                        file.toString().length());
                File newFile = new File(this.endPath + "\\" + ss);
                String ssss = file.getParent().substring(3);
                File parentFile = new File(this.endPath + "\\" + ssss);
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(fis);

                    FileOutputStream fos = new FileOutputStream(newFile);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);

                    byte[] buff = new byte[1024];
                    int temp = 0;
                    while ((temp = bis.read(buff)) != -1) {
                        bos.write(buff, 0, temp);
                        bos.flush();
                    }
                    bos.close();
                    fos.close();
                    bis.close();
                    fis.close();
                } else {
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(fis);

                    FileOutputStream fos = new FileOutputStream(newFile);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);

                    byte[] buff = new byte[1024];
                    int temp = 0;
                    while ((temp = bis.read(buff)) != -1) {
                        bos.write(buff, 0, temp);
                        bos.flush();
                    }
                    bos.close();
                    fos.close();
                    bis.close();
                    fis.close();
                }

            }
        }
    } else {
        System.out.println("您指定的文件不存在,程序结束。");
    }
}

javaIo——运用字节流完整复制文件夹及文件

标签:

原文地址:http://blog.csdn.net/sinat_33713995/article/details/51372362

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!