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()));
			}
		}
	}

}
最近下载更多
无名氏111  LV32 2023年5月23日
1090032627  LV2 2021年8月14日
帅气你虎哥  LV7 2021年7月5日
崔牛逼  LV8 2020年12月18日
擦鞭你最帅  LV6 2020年11月30日
huigui  LV2 2020年6月15日
xu1158450076  LV2 2019年11月27日
oldfox  LV18 2019年3月23日
han108  LV16 2019年3月6日
heart426  LV11 2018年10月13日
最近浏览更多
WBelong  LV7 2023年12月19日
无名氏111  LV32 2023年5月23日
yyyyyyzh  LV8 2022年12月9日
yuanye985 2022年1月11日
暂无贡献等级
tangjj7260  LV18 2021年12月25日
ideaCode  LV4 2021年9月29日
cc900118  LV17 2021年8月26日
雨凤凰  LV4 2021年8月25日
1090032627  LV2 2021年8月14日
帅气你虎哥  LV7 2021年7月5日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友