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();
}

}

java 复制指定目录中的所有文件和文件夹到另一个指定文件夹中的更多相关文章

  1. python实践项目十:zipfile模块-将一个文件夹备份到一个 ZIP 文件

    描述:将指定路径下的某文件夹备份到一个zip文件 代码: #!/usr/bin/python # -*- coding: UTF-8 -*- # backupToZip - Copies an ent ...

  2. svg矢量图标在html中的使用, (知识点:1.通过h5中的css实现点击变色,2.一个svg文件包含多个图标)

    svg矢量文件体积小,不变形,比传统的png先进,比现在流行的icon-font灵活.然而在使用过程中还是遇到了很多坑.今天花了一天时间把经验整理出来,以供后来者借鉴.如果您从本文收益,请留言mark ...

  3. 在C#中如何将多个rtf文件内容组合在一起用一个rtf文件保存?

    //重点为是使用 SelectedRtf 属性 private void button1_Click( object sender, EventArgs e ) { //mergeRTF为并内容后的 ...

  4. Python实践练习:将一个文件夹备份到一个 ZIP 文件

    题目 项目要求:假定你正在做一个项目,它的文件保存在 C:\AlsPythonBook 文件夹中.你担心工作会丢失, 所以希望为整个文件夹创建一个 ZIP 文件, 作为"快照" . ...

  5. 批处理判断是否存在文件,存在则运行另外一个bat文件

    现在需求如下: 使用bat文件判断是否存在ktr文件,存在则运行pan.bat,执行kettle脚本. 代码如下: @echo off @title 批处理判断文件夹是否存在 cd /d F: rem ...

  6. 复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中

    cp -r /etc/p*[a-Z] /tmp/mytest1

  7. Excel 表格中根据某一列的值从另一个xls文件的对应sheet中查找包含其中一列的内容(有点拗口)

    =VLOOKUP(C3&"*",INDIRECT("'[2008-2016年三地商务明细表.xls]"&L3&"年北京'!$D ...

  8. ean13码的生成,python读取csv中数据并处理返回并写入到另一个csv文件中

    # -*- coding: utf-8 -*- import math import re import csv import repr def ean_checksum(eancode): &quo ...

  9. 批量定时任务将rtf文件转为docx,入参是rtf文件夹,生成一个docx文件夹

    java,python等语言对于rft的处理很受限,rtf提供了很少的api供外部调用处理,但是对于docx我们却又很多api来处理,所以很多人会有需求将rtf批量转为docx的需求,接下来就来说说解 ...

随机推荐

  1. centos7下glances系统监控的安装

    yum install epel* -y yum install python-pip python-devel -y yum install glances -y 启动>>glances

  2. redis之订阅功能

    redis订阅 Redis 通过 PUBLISH . SUBSCRIBE 等命令实现了订阅与发布模式. 举例1: qq群的公告,单个发布者,多个收听者 发布/订阅 实验 命令 PUBLISH chan ...

  3. sed基础

    sed  文本流编辑的  行编辑器 hold space :保持空间.仓库,半成品 一次从文件中读取一行,放到自己编辑的内存缓冲空间即模式空间,不会编辑原文件:根据所给定的命令在模式空间中做编辑处理, ...

  4. codeforces gym #101987K -TV ShowGame(2-SAT)

    题目链接: https://codeforces.com/gym/101987 题意: 有长度为$n$的只包含$B,R$的字符串 有m种关系,每个关系说出三个位置的确切字符 这三个位置的字符最多有一个 ...

  5. 二、navicat连接本地数据库以及远程数据库

    本地连接 1.打开navicat 2.连接 最后点击确定就连接成功了: 远程数据库 和上面一样.....

  6. context_processor 上下文处理器

    context_processor 上下文处理器 博客里面有三个地方用到了标签云:主页面,分类页面,博客详情页面,于是有了下面一段代码 # 主页面 @main.route("/") ...

  7. 用VLC读取摄像头产生RTSP流,DSS主动取流转发(一)

    用VLC读取摄像头产生RTSP流,DSS主动取流转发(一) 摄像机地址是192.1.101.51,VLC运行在192.1.101.77上,DSS服务器架设在192.1.101.30上. Step1:V ...

  8. python图论包networks(最短路,最小生成树带包)

    官方文档: https://networkx.github.io/documentation/networkx-1.10/reference/algorithms.html 最短路和最小生成树: im ...

  9. pch文件的添加

    想说试了好久一直报错找不到文件,解决方法如下: 依次是:./项目名/文件夹名称/pch文件名

  10. WPS--world使用格式刷

    1.在空白处左键点击一下即可出现格式刷