package com.jackie.io.readfile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 文件的操作
* 1、文件的拷贝
* 2、文件夹的拷贝
* @author liutao
* @date 2017年12月11日 上午11:11:58
*/
public class FileUtils {
/**
* 文件的复制操作
* @param srcPath 源文件路径
* @param destPath 目标文件路径
* @throws IOException
*/
public void copyFile(String srcPath,String destPath) throws IOException{
/*//建立联系
File src = new File(srcPath);
File dest = new File(destPath);
//选择流
InputStream is = null;
OutputStream os = null;
byte[] byteArr = new byte[1024];//默认的读取和接收的字节数
int len = 0;//实际接收和读取的字节数
try {
//操作
is = new FileInputStream(src);
os = new FileOutputStream(dest);
while(-1 != (len =is.read(byteArr))){
os.write(byteArr, 0, len);
}
os.flush();//强制刷出
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/
copyFile(new File(srcPath), new File(destPath));
}
/**
* 文件的复制
* @param src 源文件的File对象
* @param dest 目标文件的File对象
* @throws IOException IO流异常
*/
public void copyFile(File src,File dest) throws IOException{
if( !src.isFile() ){
throw new IOException("您所选择的不是文件,请您重新选择!");
}
if(dest.isDirectory()){
throw new IOException("不能建立与文件夹" + dest.getAbsolutePath() + "名称重复的文件");
}
//选择流
InputStream is = null;
OutputStream os = null;
byte[] byteArr = new byte[1024];//默认的读取和接收的字节数
int len = 0;//实际接收和读取的字节数
try {
//操作
is = new FileInputStream(src);
os = new FileOutputStream(dest);
while(-1 != (len =is.read(byteArr))){
os.write(byteArr, 0, len);
}
os.flush();//强制刷出
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 文件夹的拷贝
* @param srcPath 源文件夹路径
* @param destPath 目标文件夹路径
* @throws IOException IO一异常
*/
public void copyDir(String srcPath,String destPath) throws IOException{
copyDir(new File(srcPath),new File(destPath));
}
/**
* 文件夹的拷贝
* @param src 原文件夹file对象
* @param dest 目标文件夹对象
* @throws IOException IO异常
*/
public void copyDir(File src,File dest) throws IOException{
if(src.isDirectory()){
dest = new File(dest,src.getName());
}
copyDirDetails(src,dest);
}
/**
* 拷贝文件夹层级获取
* @param src 原文件夹路径 file对象
* @param dest 目标文件夹路径file对象
* @throws IOException IO异常
*/
public void copyDirDetails(File src,File dest) throws IOException {
if(src.isFile()){
copyFile(src, dest);
}else if(src.isDirectory()){
dest.mkdirs();//确保目标文件的存在
//获取下一级目录|文件
for(File sub:src.listFiles()){
copyDirDetails(sub,new File(dest,sub.getName()));
}
}
}
/**
*
* @param srcPath
* @param destPath
* @throws IOException
*/
public void copyDirDetails(String srcPath,String destPath) throws IOException {
if(new File(srcPath).isFile()){
copyFile(new File(srcPath), new File(destPath));
}else if(new File(srcPath).isDirectory()){
new File(destPath).mkdirs();//确保目标文件的存在
//获取下一级目录|文件
for(File sub:new File(srcPath).listFiles()){
copyDirDetails(sub,new File(new File(destPath),sub.getName()));
}
}
}
}