/*
将"C:\\JavaProducts\\Source"下的所有数据复制到"C:\\Target"下
*/ import java.io.*; public class JavaCopyDemo{
final static String SOURCESTRING = "C:\\JavaProducts\\Source";
final static String TARGETSTRING = "C:\\Target"; public static void main(String[] args){
if(!(new File(SOURCESTRING)).exists()){
System.out.println("源文件" + SOURCESTRING + "不存在,无法复制!");
return;
}else if((new File(TARGETSTRING)).exists()){
System.out.println("目标文件" + TARGETSTRING + "已经存在,无法复制!");
return;
}else{
if((new File(SOURCESTRING)).isFile()){
copyFile(new File(SOURCESTRING),new File(TARGETSTRING));
}else if((new File(SOURCESTRING)).isDirectory()){
copyDirectory(SOURCESTRING,TARGETSTRING);
}
}
} private static void copyFile(File sourceFile,File targetFile){
if(!sourceFile.canRead()){
System.out.println("源文件" + sourceFile.getAbsolutePath() + "不可读,无法复制!");
return;
}else{
System.out.println("开始复制文件" + sourceFile.getAbsolutePath() + "到" + targetFile.getAbsolutePath());
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null; try{
fis = new FileInputStream(sourceFile);
bis = new BufferedInputStream(fis);
fos = new FileOutputStream(targetFile);
bos = new BufferedOutputStream(fos);
int len = 0;
while((len = bis.read()) != -1){
bos.write(len);
}
bos.flush(); }catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(fis != null){
fis.close();
}
if(bis != null){
bis.close();
}
if(fos != null){
fos.close();
}
if(bos != null){
bos.close();
}
System.out.println("文件" + sourceFile.getAbsolutePath() + "复制到" + targetFile.getAbsolutePath() + "完成");
}catch(IOException e){
e.printStackTrace();
}
}
}
} private static void copyDirectory(String sourcePathString,String targetPathString){
if(!new File(sourcePathString).canRead()){
System.out.println("源文件夹" + sourcePathString + "不可读,无法复制!");
return;
}else{
(new File(targetPathString)).mkdirs();
System.out.println("开始复制文件夹" + sourcePathString + "到" + targetPathString);
File[] files = new File(sourcePathString).listFiles();
for(int i = 0; i < files.length; i++){
if(files[i].isFile()){
copyFile(new File(sourcePathString + File.separator + files[i].getName()),new File(targetPathString + File.separator + files[i].getName()));
}else if(files[i].isDirectory()){
copyDirectory(sourcePathString + File.separator + files[i].getName(),targetPathString + File.separator + files[i].getName());
}
}
System.out.println("复制文件夹" + sourcePathString + "到" + targetPathString + "结束");
}
}
}

Java将一个目录下的所有数据复制到另一个目录下的更多相关文章

  1. Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹

    Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹     1.将一个文件夹下的所有内容复制到另一个文件夹下 cp -r /home/packageA/* /home/cp/packageB ...

  2. SQL把表中的数据复制到另一个数据库中

    1 删除整张表的数据,并还原自增长值TRUNCATE TABLE TbWeixinActivity 2 3张表左连接select a.ID,c.Name,b.nickname,a.CreateDate ...

  3. SQL数据库中把一个表中的数据复制到另一个表中

    1.如果是整个表复制表达如下: insert into table1 select  * from table2 2.如果是有选择性的复制数据表达如下: insert into table1(colu ...

  4. 将一个Head下的Line复制到另一个Head下(ef+linq)

    今天工作中有一个需求,要求将一个Item下的Line复制到另外一个Item下面 这个需求在工作中很多,按照以往的经验肯定是先delete from,然后再一条条遍历后insert into 这两天正好 ...

  5. SQL SERVER 将一个数据库中的表和数据复制到另一个数据库中

    第一种情况:将A数据库.dbo.A表的数据追加到B数据库.dbo.B表中 (条件:此时B数据库中已创建好了B表) insert into B数据库.dbo.B表 select * from A数据库. ...

  6. Linux 将文件夹下的所有文件复制到另一个文件里

    如何将文件夹/home/work下的文件复制到/home/temp里面? 使用命令: cp -R /home/work/* /home/temp *表示所有文件 但是/home/work 下的隐藏文件 ...

  7. oracle把一个表的数据复制到另一个表中

    http://blog.csdn.net/my_name_nb/article/details/64128015 ........................ 1. 新增一个表,通过另一个表的结构 ...

  8. mui实现分页上拉加载更多 下拉刷新数据的简单实现 移动端下拉上拉

    空下来把mui上拉加载更多,下拉刷新数据做了一个简单的实现,希望可以帮助到需要的朋友 demo项目的结构 <!DOCTYPE html> <html> <head> ...

  9. 把一个List<T>的数据复制至另一个List<T>

    把一个数据集List<T>复制至到另一个数据集List<T>. 方法一,可以使用循环,然后把每一个T添加至另一个集合中去: public void ListDemo() { , ...

随机推荐

  1. [前言] 实现一个Android电子书阅读APP

    大家好,我是小方,我将在接下来的几篇文章中从零实现一个网络小说阅读器,从安卓编程最基础的部分讲起,直至成功完成我们的应用,从新建一个项目开始,不断添加新的代码,添加新的界面,循序渐进,涵盖所有我们需要 ...

  2. lxd容器之GPU发现和加载

    lxd gpu设备发现: // /dev/nvidia[0-9]+ type nvidiaGpuCards struct { path string major int minor int id st ...

  3. How to build mscorlib.dll with visual studio

    Recently, Microsoft Corportation has released a new look for .NET Reference Source. And you may find ...

  4. React-Native 开发(一) Android环境部署,Hello react-native

    前提: 一个小web前端,完全不会android 跟iOS 的开发,首次接触,有很多不懂的问题.请见谅 环境: win7 成果:                           一.SDK安装 提 ...

  5. SEO-站内优化规范

    类别 要求 实际工作要求 程 序 设 计 1.DIV+CSS布局 2.站内导航连接性良好 面包屑导航,翻页方式使用样式二,文章和产品上一页和下一页 3.图片的ALT属性 在编程时注意写 4.超级链接的 ...

  6. RabbitMQ-从基础到实战(2)— 防止消息丢失

    转载请注明出处 1.简介 RabbitMQ中,消息丢失可以简单的分为两种:客户端丢失和服务端丢失.针对这两种消息丢失,RabbitMQ都给出了相应的解决方案. 2.防止客户端丢失消息 如图,生产者P向 ...

  7. 2017-3-17 SQL server 数据库 视图,事务,备份还原,分离附加

    1.视图:只能查看,不能增删改不能有重复列 create view 名字as查询语句 2.事务:保证流程的完整执行 begin tran --开始事务监控 被监控的代码 ... ...if @@ERR ...

  8. 分享自己使用CSS的public

    body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select,td,figure{marg ...

  9. Java引领新生活

    阅读邹欣老师的博客,谈谈你期望的师生关系是什么样的? 我觉得师生关系应当是亲密无间,课上老师讲解学生配合,课下师生交流启发思考. 你有什么技能(学习,棋类,球类,乐器,艺术,游戏,......)比大多 ...

  10. Java的一些基础知识

    1.do-while循环不论循环条件判断结果,至少会执行一次. 2.Javac用于Java源代码文件编译成字节码的编译器. 3.import关键字导入包. 4.Java是Sun公司与1995年退出的高 ...