/*
将"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. [TPYBoard - Micropython] 五分种学会用TPYBoard - GPS 制作短信群发机

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604   一.什么是TPYBoard-GPS TPYBoard-GPS又称TPYBoa ...

  2. 【2017-02-23】switch...case...和for循环

    1.代码简化折叠: #region 标题 ... ... #endregion 一.switch...case... 1.格式 switch(变量){ case 值:代码段;break; case 值 ...

  3. Chosen通用初始化

    一直在用Chosen这个js插件,其目的就是美化下拉框.github地址:https://harvesthq.github.io/chosen/ no_results_text:"xxxxx ...

  4. appium初学者,使用之检查appium环境报错Could not detect Mac OS X Version from sw_vers output: '10.12.1’,

    这个问题存在的原因就是appium不兼容最新的mac10.12版本. 由于10.12是最新mac版本,appium1.5.3并未提供支持,所以: 在终端输入grep -rl "Could n ...

  5. Struts2中Action配置的三种方式

    <!-- 方案一:一个action对应一个方法; --> <action name="add" class="com.gxxy.struts.kp03_ ...

  6. 2764: [JLOI2011]基因补全

    2764: [JLOI2011]基因补全 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 570  Solved: 187[Submit][Status ...

  7. BananaPi python-Mysql 操作库

    BananPi python-Mysql 操作库 1.首先mysql.python环境安装 2.下载MySQL-python-1.2.3.tar.gz 并解压 tar xfz MySQL-python ...

  8. django进阶补充

    前言: 这篇博客对上篇博客django进阶作下补充. 一.效果图 前端界面较简单(丑),有两个功能: 从数据库中取出书名 eg: 新书A 在form表单输入书名,选择出版社,选择作者(多选),输入完毕 ...

  9. 如何在RecyclerView上面实现"拖放"和"滑动删除"-1

    Android上面有许多的教程, 库和示例, 在RecyclerView上面实现"拖放"和"滑动删除"功能. 尽管有更新, 更好的方法可用, 但是大多数人依然使 ...

  10. 【SF】开源的.NET CORE 基础管理系统系列导航

    SF是一套基于ASP.NET Core MVC+EFCore+Bootstrap开发出来的基础管理系统,源代码完全开源,可以帮助你解决C#.NET项目中重复编码的繁琐工作,让开发人员远离加班! SF ...