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 ...
随机推荐
- OpenCV 特征描述
#include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #inclu ...
- python 入门手册
python 入门手册 Introduction 这个手册是为了让学习者好好的重塑对于编程的认识,我们要来认识到怎么来在陌生的领域学习,学习的技巧开始,通过官方文档来学习 开端 利用IDE输出&quo ...
- 专利|Pct||
专利:有些专利写的尽量模糊,为了不让别人检出,让别人能轻易侵犯专利权 优先权:在本国申请后,在他国也是同一个专利人申请,并也是同一个申请日. 发明20年:实用新型外观设计:20年 Pct:专利合作条约 ...
- [LC] 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- JavaScript学习总结(六)数据类型和JSON格式
转自:http://segmentfault.com/a/1190000000668072 什么是JSON JSON:JavaScript 对象表示法(JavaScript Object Notati ...
- JavaScript学习总结(四)function函数部分
转自:http://segmentfault.com/a/1190000000660786 概念 函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. js 支持两种函数:一类是语言内部的函数 ...
- python3下scrapy爬虫(第三卷:初步抓取网页内容之抓取网页里的指定数据)
上一卷中我们抓取了网页的所有内容,现在我们抓取下网页的图片名称以及连接 现在我再新建个爬虫文件,名称设置为crawler2 做爬虫的朋友应该知道,网页里的数据都是用文本或者块级标签包裹着的,scrap ...
- GYOJ_1812_股票(stock)
题目描述 2130年,股神巴菲特投胎了!他投胎到你身上! 你作为股神转世,能力比原股神还要强,你可以预测到今后n天的股价.假设刚开始你的手上有1元钱,你想知道n天后你最多可以赚到多少钱.作为股神转世, ...
- Java如何打印日志
以下为<正确的打日志姿势>学习笔记. 什么时候打日志 1.程序出现问题,只能通过 debug 功能来定位问题,很大程度是日志没打好.良好的系统,通过日志就能进行问题定位. 2.if-els ...
- JavaScript常见排序算法
1.冒泡排序 function bubble_sort(arr) { if (arr.length <= 1) { return arr; } var len = arr.length; for ...