java文件的拷贝函数_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)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值