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 ...
随机推荐
- deeplearning.ai 序列模型 Week 2 NLP & Word Embeddings
1. Word representation One-hot representation的缺点:把每个单词独立对待,导致对相关词的泛化能力不强.比如训练出“I want a glass of ora ...
- AD复制问题汇总
1:文件复制服务NtFrs 13568报错的解决方法 解决方法: 建议不要按照日志的提示进行操作,正确的操作应该是 出现这个问题的原因,是由于在硬件的损坏,导致服务器未正确处理NTFS USN 日志. ...
- Protein interaction|insight QUANTA|SYBYL COMPOSER|MODELLER|SWISS_MODEL|WHAT IF|3D-JIGSAW|CPH-ModelGPCRs|Membrane protein|
生命组学 蛋白质之间的互作可以有以下应用: Eg:改变蛋白质基因,从而组改变结构,削弱蛋白质之间的相互作用. Eg:数据模拟出蛋白质的靶点,即结合腔,将此数据存入结合化合物的dataset,用于制药 ...
- 使用iframe的好处与坏处详细比拼
一.使用iframe的坏处 1.搜索引擎的蜘蛛不会识别在iframe中被调用的图片.文本.url等内容的,因为该内容不属于该页面,只是访问的时候被临时的调用,而且在SEO建议中也有提到:"f ...
- MOOC(1)-使用pycharm新建Django项目、开发post接口
https://www.cnblogs.com/liqu/p/9308966.html 1.安装Django的两种方式: > 1) pip install django 2)下载离线安装包,进入 ...
- Nginx笔记总结十三:nginx 正向代理
server { listen ; location / { resolver 202.106.0.20 202.106.119.116; resolver_timeout 30s; proxy_pa ...
- Hello 2015
"Yeah It's on. " 前言 Hux 的 Blog 就这么开通了. 跳过废话,直接看技术实现 2015 年,Hux 总算有个地方可以好好写点东西了. 作为一个程序员, B ...
- JMeter之BeanShell断言---获取时间戳
1.创建线程组,创建一个BeanShell Sampler,在其中编写BeanShell脚本. 2.在Jmeter中,可以利用${__time(,)}时间戳函数来获取十位的时间戳,如: vars.pu ...
- test 5.1 高精度小数(10分)
请写一个程序,输入一个分数,计算出它的小数形式.无论是否可以除尽,输出最多小数点后200位. 题目内容 由于计算机内部表达方式的限制,浮点运算都有精度问题,为了得到高精度的计算结果,就需要自己设计实现 ...
- Flask添加新命令
代码: import click from flask import Flask app = Flask(__name__) @app.cli.command() def hg(): click.ec ...