您的位置:首页 > 编程语言 > Java开发

java 文件复制 文件夹复制工具类

2016-07-13 17:44 447 查看
自己写的工具类,有不对的地方或者可以优化的地方,希望留言,共同讨论

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;

public class CopyFileDirUtil {
public static  int IS_EXISTS  = 0;
public static void main(String[] args) {
//		String sourcePath = "D:/study_project/java-io-test.txt";
//		String targetPath = "D:/study_project/java-io-test-to.txt";
String sourcePath = "D:/study_project/test";
String targetPath = "D:/study_project/copyTest";
//		copyFile(sourcePath,targetPath);
copyDirectory(sourcePath, targetPath);
}
/**
* 文件的拷贝
* @param sourcePath
* @param targetPath
*/
public static void copyFile(String sourcePath,String targetPath){
File sourceFile = new File(sourcePath);
File targetFile = new File(targetPath);
copyFile(sourceFile, targetFile);
}
/**
* 文件的拷贝
* @param sourceFile
* @param targetFile
*/
public static void copyFile(File sourceFile,File targetFile){
if(!sourceFile.exists()){
System.out.println("源文件没有找到");
return;
}
if(targetFile.exists()){
System.out.println("目标文件已经存在,删除已存在的目标文件");
targetFile.delete();
}
BufferedInputStream fis = null;
BufferedOutputStream fos = null;
int len = 100;
int count = -1;
byte[] buffer = new byte[len];
try {
fis = new BufferedInputStream(new FileInputStream(sourceFile));
fos = new BufferedOutputStream(new FileOutputStream(targetFile));
while ((count=fis.read(buffer, 0, len))!= -1) {
fos.write(buffer, 0, count);
}
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件没有找到");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取异常");
} finally {
try {
fis.close();
fos.close();
} catch (Exception e) {
System.out.println("流关闭异常");
}
}
}
/**
* 文件夹拷贝
* @param sourcePath
* @param targetPath
*/
public static void copyDirectory(String sourcePath,String targetPath){
File sourceFile = new File(sourcePath);
File targetFile = new File(targetPath);
copyDirectory(sourceFile, targetFile);
}
/**
* 文件夹拷贝
* @param sourcePath
* @param targetPath
*/
public static void copyDirectory(File sourceFile,File targetFile){
if(!sourceFile.exists()){
System.out.println("源文件没有找到");
return;
}
if(targetFile.exists() && IS_EXISTS == 0){
System.out.println("目标文件已经存在,删除已存在的目标文件");
targetFile.delete();
IS_EXISTS = 1;
}
if(sourceFile.isDirectory()){
targetFile.mkdir();
}
if(sourceFile.isDirectory()){
File[] listFile = sourceFile.listFiles();
for(File file : listFile){
String newPath = file.getPath();
String newCopyPath = targetFile.getPath()+File.separator+file.getName();
if(file.isDirectory()){
copyDirectory(newPath, newCopyPath);
}else{
copyFile(newPath, newCopyPath);
}
}
}else{
copyFile(sourceFile, targetFile);
}
}

}

拷贝文件和文件夹增强健壮性,修改优化后的结果如下:

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;

public class CopyUtil {

/**
* @param args
*/
public static void main(String[] args) {
String source = "D:/2";//源
String target = "D:/3";//目标
File sou = new File(source);
File tar = new File(target);
if(sou.isDirectory()){
//在目标文件下,创建源文件文件夹
tar = new File(tar,sou.getName());
}
//拷贝文件
copyDir(sou, tar);
}
/**
* 拷贝文件夹
* @param source
* @param target
*/
public static void copyDir(File source,File target){
if(source.isFile()){//如果源是一个文件
//拷贝文件
copyFile(source,target);
}else if(source.isDirectory()){//如果源是一个文件夹
//目标下创建文件夹
target.mkdirs();
//递归循环查找该文件夹下的所有文件和文件夹
for(File sub : source.listFiles()){
copyDir(sub,new File(target,sub.getName()));
}
}
}
/**
* 拷贝文件
* @param sourceFile
* @param targetFile
*/
public static void copyFile(File sourceFile,File targetFile){

if(targetFile.exists()){
System.out.println("目标文件已经存在,删除已存在的目标文件");
targetFile.delete();
}
BufferedInputStream fis = null;
BufferedOutputStream fos = null;
int len = 100;
int count = -1;
byte[] buffer = new byte[len];
try {
fis = new BufferedInputStream(new FileInputStream(sourceFile));
fos = new BufferedOutputStream(new FileOutputStream(targetFile));
// fis.read(buffer, 0, len) 每次最多读取len这个长度的数据,放入到buffer自己数据中,赋值给变量count,如果数据读取完毕,count=-1,作为推出循环的条件
while ((count=fis.read(buffer, 0, len))!= -1) {
// 缓冲写出数据
fos.write(buffer, 0, count);
}
// 强制刷新数据到磁盘
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件没有找到");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取异常");
} finally {
try {
fis.close();
fos.close();
} catch (Exception e) {
System.out.println("流关闭异常");
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息