java 复制指定目录中的所有文件和文件夹到另一个指定文件夹中
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class CopyFile {
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:/test/A";
String newPath = "D:/test/B";
copyDir(sourcePath, newPath);
}
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], newPath + file.separator + filePath[i]);
}
if (new File(sourcePath + file.separator + filePath[i]).isFile()) {
copyFile(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
}
}
}
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();
}
}
最新文章
- [版本管理]有惊无险修复svn服务器Invalid filesystem revision number问题
- openldap复制
- LevelDB(v1.3) 源码阅读之 Arena(内存管理器)
- 基于Spring框架的Web应用开发笔记 - Outline
- IIS负载均衡相关
- Setup a private http/nginx based GIT server
- 浅谈如何让 Bootstrap 3兼容IE8浏览器
- java集合练习
- bzoj:1457: 棋盘游戏
- python arvg用法
- NancyFX 第五章 Nancy 路由
- 【IOS 开发】Object - C 语法 之 流程控制
- Centos6.5DRBD加载失败,系统更换yum源(国内163)
- 如何用cmake编译
- git 提交代码操作
- LeetCode算法题-Two Sum II - Input array is sorted
- How to create an rpm package
- 手机CPU天梯图2018年5月最新版
- TestNG参数
- Centos7初次开机提示Initial setup of CentOS Linux 7 (core)