Java中将文件夹复制到另一个文件夹
- 文件夹的拷贝
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)。
Java中将文件夹复制到另一个文件夹的更多相关文章
- Java基础面试操作题: File IO 文件过滤器FileFilter 练习 把一个文件夹下的.java文件复制到另一个文件夹下的.txt文件
package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...
- Linux 将文件夹下的所有文件复制到另一个文件里
如何将文件夹/home/work下的文件复制到/home/temp里面? 使用命令: cp -R /home/work/* /home/temp *表示所有文件 但是/home/work 下的隐藏文件 ...
- Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹
Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹 1.将一个文件夹下的所有内容复制到另一个文件夹下 cp -r /home/packageA/* /home/cp/packageB ...
- C# 将文件夹中文件复制到另一个文件夹
p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...
- C# 把一个文件夹下所有文件复制到另一个文件夹下 把一个文件夹下所有文件删除(转)
C# 把一个文件夹下所有文件复制到另一个文件夹下 public static void CopyDirectory(string srcPath, string destPath) { try { ...
- java把一个文件的内容复制到另外一个文件
/** * java把一个文件的内容复制到另外一个文件 */import java.io.File;import java.io.FileInputStream;import java.io.File ...
- Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- Path,Files巩固,题目:从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
这个题目用传统的File,InputStream可以做,但是如果用Files,Path类做,虽然思路上会困难一些,但是代码简洁了很多,以下是代码: import java.io.IOException ...
- 通过java 来实现对多个文件的内容合并到一个文件中
现在有多个txt文本文件,需要把这么多个文件的内容都放到一个文件中去 以下是实现代码 package com.SBgong.test; import java.io.*; public class F ...
随机推荐
- [LC] 66. Plus One
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...
- DIP|PCN|CoevDB|PID|Y2H|RosettaDock Serve|元基因组学|微生物多样性
生命组学: 比较真核生物有关呼吸链的gene是比较核外编码基因,因为与呼吸有关的功能在线粒体上,线粒体位于核外.想要查看两种基因是否具有相互作用,可以对不同物种的编码ATP6 和ATP8的直系同源基因 ...
- Spring Boot 集成 Spring Security
1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Json格式化的实现(Jackson、Gson)
一.第一种(Jackson) 需要用到的jar包: https://pan.baidu.com/s/1wrkUwEoKpmqgmYPQSN-iZg package util; import com.f ...
- CodeFroces New Assignment 二分图匹配
There is a class consisting of n students, in which each one has a number representing his/her perso ...
- poi报表导出4.1.0版本工具类 导出并下载
这一段时间,由于项目上线基于稳定,所以我这边在基于我们一期迭代的分支上优化一部分我们之前没有做的功能,报表导出.本身之前用的是3.5的版本,但是由于同事要写导入,写的代码只有4.1.0的版本支持,所以 ...
- 测试用例设计经典面试题之电梯、杯子、笔、桌子、洗衣机、椅子、ATM等
测试用例设计经典面试题之电梯.杯子.笔.桌子.洗衣机.椅子.ATM等 1.测试项目:电梯 需求测试:查看电梯使用说明书.安全说明书等 界面测试:查看电梯外观 功能测试:测试电梯能否实现正常的上升和下降 ...
- Thomson Plaza里面的三家店以及水果大会
旅行应该是一个发现的过程,至少我是这么认为的.很多时候并不一定要到什么特别的地方,也可以感受到旅游的乐趣.我觉得只要能看到值得回味的东西就好了.而能回味的东西,往往是需要仔细地来品.像旅行社安排的那样 ...
- 成为数据专家,你只差一个Quick Insights的距离
身处如今的大数据时代,你真的知道如何处理数据和分析数据吗?或许那些被你忽视的数据背后就暗藏着重要的商业灵感.并非人人都是数据专家,有时候你需要一些专业的软件来帮你处理数据.那么如何能快速.准确地从数据 ...
- -scp Linux之间复制文件和目录
scp 简介 scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速 ...