拷贝文件目录(可根据后缀过滤文件)

package com.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Pattern;
/**
 * 拷贝一个目录下的文件
 * @author Administrator
 *
 */
public class CopyFiles {
	/**
	 * 复制目录(复制后的文件目录文件与源文件目录一致)
	 * @param fromDir 要复制的源文件目录
	 * @param toDir  复制文件的目的地目录
	 * @param fileType 过滤要复制的源文件的类型(只复制所选择的类型)
	 * @throws IOException
	 */
	public void copyDir(String fromDir,String toDir,String fileType) throws IOException{
		if(null != fromDir && null!=toDir){
			if(!new File(toDir).exists()){
				new File(toDir).mkdirs();
			}
			File fromP = new File(fromDir);
			if(fromP.isDirectory()){
				File[] fromPs = fromP.listFiles();
				for (File f : fromPs) {
					if(f.isDirectory()){
						String newFromDir = f.getAbsolutePath();
						String newToDir = toDir + newFromDir.substring(fromDir.length());
						if(!(new File(newToDir)).exists()){
							(new File(newToDir)).mkdir();
						}
						copyDir(newFromDir,newToDir,fileType);
					}else if(f.isFile()){
						String fileName = f.getName();
						String newToDir = toDir+"\\"+fileName;
						copyFile(f.getAbsolutePath(),newToDir,fileType);
					}
				}
			}else if(fromP.isFile()){
				String fileName = fromP.getName();
				String newToDir = toDir+"\\"+fileName;
				copyFile(fromP.getAbsolutePath(),newToDir,fileType);
			}
		}
	}
	/**
	 * 复制目录(直接将要复制的文件放置于目的地目录中,即与源文件目录不一致)
	 * @param fromDir 要复制的源文件目录
	 * @param toDir  复制文件的目的地目录
	 * @param fileType 过滤要复制的源文件的类型(只复制所选择的类型)
	 * @throws IOException
	 */
	public void copyDir2(String fromDir,String toDir,String fileType) throws IOException{
		if(null != fromDir && null!=toDir){
			if(!new File(toDir).exists()){
				new File(toDir).mkdirs();
			}
			File fromP = new File(fromDir);
			if(fromP.isDirectory()){
				File[] fromPs = fromP.listFiles();
				for (File f : fromPs) {
					if(f.isDirectory()){
						String newFromDir = f.getAbsolutePath();
						String newToDir = toDir;//即目的地目录
						if(!(new File(newToDir)).exists()){
							(new File(newToDir)).mkdir();
						}
						copyDir2(newFromDir,newToDir,fileType);
					}else if(f.isFile()){
						String fileName = f.getName();
						String newToDir = toDir+"\\"+fileName;
						copyFile(f.getAbsolutePath(),newToDir,fileType);
					}
				}
			}else if(fromP.isFile()){
				String fileName = fromP.getName();
				String newToDir = toDir+"\\"+fileName;
				copyFile(fromP.getAbsolutePath(),newToDir,fileType);
			}
		}
	}
	/**
	 * 复制文件
	 * @param fromFile 要复制的文件路径
	 * @param toFile 复制文件的目的地
	 * @param fileType 过滤要复制的文件的类型(如果传入null或者""则复制全部文件,否则只复制所选择的类型)
	 * @throws IOException
	 */
	public void copyFile(String fromFile,String toFile,String fileType) throws IOException{
		if(null==fileType || "".equalsIgnoreCase(fileType) || fromFile.endsWith(fileType)){			
			toFile=createNewExistFile(toFile);
			FileInputStream fromFIS = new FileInputStream(fromFile);
			FileOutputStream toFOS = new FileOutputStream(toFile);
			int i=1024;
			byte[] b = new byte[i];
			while(fromFIS.read(b, 0, 1024)!=-1 && i>0){
				toFOS.write(b);
				toFOS.flush();
			}
			toFOS.close();
			fromFIS.close();
		}			
	}
	
	/**
	 * 如果文件是否在路径已经存在,存在的话更改名字后重新创建并返回
	 * @param filePath 文件路径
	 * @return 更改名字后重新创建并返回
	 * @throws IOException
	 */
	private String createNewExistFile(String filePath) throws IOException {
		// TODO Auto-generated method stub
		File file = new File(filePath);
		if(!file.exists()){
			file.createNewFile();
			return filePath;
		}else{
			String fileName = file.getName();
			String fileSimpleName = (fileName.substring(0, fileName.lastIndexOf(".")));
			String fileType = (fileName.substring(fileName.lastIndexOf(".")+1));
			if(Pattern.matches(".+\\(\\d+\\)",fileSimpleName)){
				fileSimpleName.lastIndexOf("\\(");
				int number = Integer.valueOf(fileSimpleName.substring(fileSimpleName.lastIndexOf("(")+1,fileSimpleName.length()-1));
				fileSimpleName=fileSimpleName.substring(0,fileSimpleName.lastIndexOf("(")+1)+(number+1)+")";
			}else{
				fileSimpleName=fileSimpleName+"(2)";
			}
			String newFilePath=filePath.substring(0, filePath.lastIndexOf("\\"))+"\\"+fileSimpleName+"."+fileType;
			return createNewExistFile(newFilePath);
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CopyFiles mj = new CopyFiles();
		try {
			//拷贝"E:\\jfinal\\jfinal-2.2-all"目录下的所有".jar"文件到"E:\\jfinal\\jfinalALLJar",并保持原目录结构
			mj.copyDir("E:\\jfinal\\jfinal-2.2-all","E:\\jfinal\\jfinalALLJar",".jar");
			//拷贝"E:\\jfinal\\jfinal-2.2-all"目录下的所有".jar"文件到"E:\\jfinal\\jfinalALLJar",并不保存原目录结构
//			mj.copyDir2("E:\\jfinal\\jfinal-2.2-all","E:\\jfinal\\jfinalALLJar",".jar");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(Pattern.matches("(2)", ".?\\(\\d+\\)"));
		System.out.println(Pattern.matches( ".+\\(\\d+\\)","a(2)"));
		System.out.println("(2)".matches(".?\\(\\d+\\)"));
		System.out.println("sfsd(sdf)sfsd(sdf)".substring("sfsd(sdf)sfsd(sdf)".lastIndexOf("(")+1,"sfsd(sdf)sfsd(sdf)".length()-1));
		System.out.println("sfsd(sdf)sfsd(sdf)".substring("sfsd(sdf)sfsd(sdf)".lastIndexOf(")")));
		System.out.println("sfsd(sdf)sfsd(sdf)".substring(0,"sfsd(sdf)sfsd(sdf)".lastIndexOf("(")+1));
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值