1. 文件夹的拷贝
public static void copyDir(String sourcePath, String newPath) {
File start = new File(sourcePath);
File end = new File(newPath);
String[] filePath = start.list(); //获取该文件夹下的所有文件以及目录的名字
if(!end.exists()) {
end.mkdir();
}
for(String temp:filePath) {
//查看其数组中每一个是文件还是文件夹
if(new File(sourcePath+File.separator+temp).isDirectory()) {
//为文件夹,进行递归
copyDir(sourcePath+File.separator+temp, newPath+File.separator+temp);
}else {
//为文件则进行拷贝
copyFile(sourcePath+File.separator+temp, newPath+File.separator+temp);
}
}
}

2.文件的拷贝

public static void copyFile(String sourcePath, String newPath) {
File start = new File(sourcePath);
File end = new File(newPath);
try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(start));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(end))) {
int len = 0;
byte[] flush = new byte[1024];
while((len=bis.read(flush)) != -1) {
bos.write(flush, 0, len);
}
bos.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}

注意:在该函数中,用到的是java7增强的try语句来进行关闭资源。它允许在try关键字后紧跟一对圆括号,里面可以声明、初始化一个或多个资源(不同的资源之间用分号隔开),此处的资源指的是那些必须在程序结束时显示关闭的资源(数据库连接、网络连接等),try语句会在该语句结束时自动关闭这些资源。

3. 函数的调用

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("From:");
String sourcePath = scanner.nextLine();
System.out.print("To:");
String newPath = scanner.nextLine();
copyDir(sourcePath, newPath);
}

4. 源代码

/**
*
* 复制文件夹d:/java下面所有文件和子文件夹内容到d:/java2。
提示:涉及单个文件复制、目录的创建、递归的使用
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner; public class Practice01{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("From:");
String sourcePath = scanner.nextLine();
System.out.print("To:");
String newPath = scanner.nextLine();
copyDir(sourcePath, newPath);
} //文件夹的拷贝
public static void copyDir(String sourcePath, String newPath) {
File start = new File(sourcePath);
File end = new File(newPath);
String[] filePath = start.list(); //获取该文件夹下的所有文件以及目录的名字
if(!end.exists()) {
end.mkdir();
}
for(String temp:filePath) {
//查看其数组中每一个是文件还是文件夹
if(new File(sourcePath+File.separator+temp).isDirectory()) {
//为文件夹,进行递归
copyDir(sourcePath+File.separator+temp, newPath+File.separator+temp);
}else {
//为文件则进行拷贝
copyFile(sourcePath+File.separator+temp, newPath+File.separator+temp);
}
}
} //文件的拷贝
public static void copyFile(String sourcePath, String newPath) {
File start = new File(sourcePath);
File end = new File(newPath);
try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(start));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(end))) {
int len = 0;
byte[] flush = new byte[1024];
while((len=bis.read(flush)) != -1) {
bos.write(flush, 0, len);
}
bos.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}

当然和文件以及流有关的更多操作,可以使用Apache Software Foundation提供的相关jar包(commons-io)。

ps:commons-io 下载地址

Java中将文件夹复制到另一个文件夹的更多相关文章

  1. Java基础面试操作题: File IO 文件过滤器FileFilter 练习 把一个文件夹下的.java文件复制到另一个文件夹下的.txt文件

    package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...

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

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

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

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

  4. C# 将文件夹中文件复制到另一个文件夹

    p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...

  5. C# 把一个文件夹下所有文件复制到另一个文件夹下 把一个文件夹下所有文件删除(转)

    C# 把一个文件夹下所有文件复制到另一个文件夹下   public static void CopyDirectory(string srcPath, string destPath) { try { ...

  6. java把一个文件的内容复制到另外一个文件

    /** * java把一个文件的内容复制到另外一个文件 */import java.io.File;import java.io.FileInputStream;import java.io.File ...

  7. Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  8. Path,Files巩固,题目:从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中

    这个题目用传统的File,InputStream可以做,但是如果用Files,Path类做,虽然思路上会困难一些,但是代码简洁了很多,以下是代码: import java.io.IOException ...

  9. 通过java 来实现对多个文件的内容合并到一个文件中

    现在有多个txt文本文件,需要把这么多个文件的内容都放到一个文件中去 以下是实现代码 package com.SBgong.test; import java.io.*; public class F ...

随机推荐

  1. .net core 读取json文件

    核心代码 Program.cs: using System; using System.IO; using Microsoft.Extensions.Configuration; namespace ...

  2. 前端-js-长期维护

    ###############    JS简介和JS引入     ################ <!DOCTYPE html> <html lang="en" ...

  3. Block to|wreck|Range|Reach|span|chase around|amuse|exploit |instructed

    English note: Block to 纷涌而至 destroy多指彻底地.毁灭性地破坏,含导致无用,不能或很难再修复的意味. wreck侧重指船只.车辆.房屋等受到严重破坏或完全毁坏,也可指计 ...

  4. Spring_IOC

    我们都知道,如果要在不同的类中使用同一个对象一般我们我们都需要在每一个类中都去new一个新的对象,也有的人会为这个对象写一个工具类,无论哪种方法都需要我们自己去创建,不但繁琐,而且相当耗损资源,所以才 ...

  5. [LC] 28. Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. 吴裕雄--天生自然HTML学习笔记:HTML <head>

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 使用 ActiveMQ 示例

    « Lighttpd(fastcgi) + web.py + MySQLdb 无法正常运行关于 Jms Topic 持久订阅 » 使用 ActiveMQ 示例 企业中各项目中相互协作的时候可能用得到消 ...

  8. VisualStudioCode通过SSH远程编辑文件

    翻译修改自:https://codepen.io/ginfuru/post/remote-editing-files-with-ssh 在远程服务器上编写文件是一件很糟糕的事情,vim和其他终端编辑器 ...

  9. 移动 H5 首屏秒开优化方案探讨

    转载bang大神文章,原文<移动 H5 首屏秒开优化方案探讨>,此文仅仅用做自学与分享! 随着移动设备性能不断增强,web 页面的性能体验逐渐变得可以接受,又因为 web 开发模式的诸多好 ...

  10. 从ArrayList的优化中想到的

    在JDK7中ArrayList有一个小的改动,使用延迟加载的思想,默认构造函数不再初始化生成一个大小为10的数组,而是将elementData先赋值为一个共享的空数组. package java.ut ...