拷贝文件目录(可根据后缀过滤文件)
来源:互联网 发布:java ftp下载文件状态 编辑:程序博客网 时间:2024/06/11 00:45
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 stubFile 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 stubCopyFiles 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 blocke.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));}}
0 0
- 拷贝文件目录(可根据后缀过滤文件)
- 文件后缀格式过滤
- 扫描当前目录并过滤一种文件后缀
- mk拷贝后缀相同的文件到指定目录命令
- Delphi 文件夹遍历所有文件包括子文件文件,并可通过后缀过滤
- python指定后缀文件拷贝
- 将指定目录下所有的固定后缀的文件拷贝到另一个目录
- Android 文件/文件目录拷贝
- 列出某文件夹及其子文件夹下面的文件,并可根据扩展名过滤
- 列出某文件夹及其子文件夹下面的文件,并可根据扩展名过滤
- 文件过滤驱动--隐藏目录
- C# 多线程目录拷贝 文件拷贝
- 拷贝文件到指定目录
- docAndoid 向目录拷贝文件
- 远程拷贝文件目录命令
- 拷贝文件到制定目录
- Android-拷贝assets目录文件
- Java 拷贝文件及目录
- [IOS 开发] iOS音频篇:使用AVPlayer播放网络音乐
- 视频 背景建模,运动物体检测
- 河内塔算法
- 【OpenGL/ES】 第04讲 glTexImage2D用法的区别
- c++2
- 拷贝文件目录(可根据后缀过滤文件)
- Mason 入门例子1 --- 基本概念及建立空仿真
- iOS海哥开发笔记(开发中如何使用数据持久化)海哥原创,让你对存储知识一目了然
- Java程序运行机制及运行过程
- eclipse安装quick text search插件,全文搜索
- 自定义简单常用的RadioButton
- git 操作
- PC与串口通信
- rpm|yum包管理|源代码包安装