博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java复制文件夹中的所有文件和文件夹到另一个文件夹中
阅读量:4463 次
发布时间:2019-06-08

本文共 1730 字,大约阅读时间需要 5 分钟。

1.复制文件夹

public static void copyDir(String sourcePath, String newPath) throws IOException {        File file = new File(sourcePath);        String[] filePath = file.list();                if (!(new File(newPath)).exists()) {            (new File(newPath)).mkdir();        }                for (int i = 0; i < filePath.length; i++) {            if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
                copyDir(sourcePath  + file.separator  + filePath[i], path  + file.separator + filePath[i]);             }                          if (new File(sourcePath  + file.separator + filePath[i]).isFile()) {
                copyFile(sourcePath + file.separator + filePath[i], path + file.separator + filePath[i]);             } } }

 

2. 复制文件的方法

public static void copyFile(String oldPath, String newPath) throws IOException {        File oldFile = new File(oldPath);        File file = new File(newPath);        FileInputStream in = new FileInputStream(oldFile);        FileOutputStream out = new FileOutputStream(file);;        byte[] buffer=new byte[2097152];        int readByte = 0;        while((readByte = in.read(buffer)) != -1){            out.write(buffer, 0, readByte);        }            in.close();      out.close();    }

3.调用方法

public static void main(String[] args) throws IOException {        Scanner sc = new Scanner(System.in);        System.out.println("请输入源目录:");        String sourcePath = sc.nextLine();        System.out.println("请输入新目录:");        String path = sc.nextLine();                //String sourcePath = "D://aa";        //String path = "D://bb";                copyDir(sourcePath, path);    }

 

转载于:https://www.cnblogs.com/lishuo/p/5786842.html

你可能感兴趣的文章
MSP430FLASH小结
查看>>
STM32 ADC转换时间
查看>>
结合实际业务场景聊一聊MVP模式的应用
查看>>
WinPE启动U盘的制作方法与软件下载(通用PE工具箱/老毛桃/大白菜WinPE)(转载)...
查看>>
行为型设计模式之5--中介者模式
查看>>
Android DevArt6:Android中IPC的六种方式
查看>>
oracle练习题
查看>>
PMP学习感想
查看>>
Zookeeper全解析——Paxos作为灵魂
查看>>
集合-强大的集合工具类:java.util.Collections中未包含的集合工具
查看>>
CSS清除浮动
查看>>
数据库基础-数据库常用命令总结
查看>>
java8 按对象属性值排序
查看>>
【转帖】国产x86处理器KX-6000发布
查看>>
04-js的运算符
查看>>
第三天 while循环 及其用法
查看>>
Delphi 10 seattle 去掉自带的代码连接线
查看>>
构建高并发高可用的电商平台架构实践(转)
查看>>
Geometry Imager Viewport Filter
查看>>
九度oj 题目1025:最大报销额
查看>>