一、文件的拷贝 1、提供能够访问到被拷贝文件及目标文件的准确路径 2、验证所提供的被拷贝文件路径是否正确是否为空 3、检查是否存在目标文件的父路径,若不存在,需重新建立 4、将所提供的路径转换成File对象 5、开始拷贝 以下是具体代码实现:
public static void copy(String srcFilePath,String destFilePath){ //检验参数合法性 if(srcFilePath==null||destFilePath==null){ throw new IllegalArgumentException("format is null"); } File srcFile=new File(srcFilePath); File destFile=new File(destFilePath); if(!srcFile.isFile()||!srcFile.exists()){//若所提供的源文件路径不正确或者不是个文件,则抛出错误 throw new IllegalArgumentException("srcFile is not a file or not exists "); } if(!destFile.getParentFile().exists()){//若提供的目标文件父目录不存在,则重新建立 destFile.getParentFile().mkdirs(); } try(FileInputStream srcFileStream=new FileInputStream(srcFile); FileOutputStream destFileStream = new FileOutputStream(destFile) ) { byte[] data=new byte[5]; int len=-1; while((len=srcFileStream.read(data))!=-1){//循环copy destFileStream.write(data,0,len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }以下是我的测试代码:
public static void main(String[] args) { String src="D:"+File.separator+"test"+File.separator+"test1.txt"; String dest="D:"+File.separator+"test"+File.separator+"test2.txt"; copy(src,dest); }运行出来的结果如下: 二、文件的合并 提供几个文件,要求将这几个文件的内容合并到一个新的文档 步骤和上面的拷贝所差无几,只是在拷贝到的时候将FileOutStream的append值改为true,将文件定义成可追加文件即可 具体实现的代码如下:
public static void merge(String[] srcFiles,String destFilepath){ if(srcFiles==null||destFilepath==null){ throw new IllegalArgumentException("format is null"); } File destFile=new File(destFilepath); if(!destFile.getParentFile().exists()){ destFile.getParentFile().mkdir(); } for(int i=0;i<srcFiles.length;i++){ if(srcFiles[i]==null){ throw new IllegalArgumentException("you have inputed a illegalargument"); } File srcFile=new File(srcFiles[i]); byte[] data=new byte[5]; int len=-1; try(FileInputStream in=new FileInputStream(srcFile); FileOutputStream out=new FileOutputStream(destFile,true)) { while((len=in.read(data))!=-1){ out.write(data,0,len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }我的测试代码如下:
public static void main(String[] args) { String[] src1={"D:"+File.separator+"test"+File.separator+"test3.txt","D:"+File.separator+"test"+File.separator+"test4.txt"}; String dest1="D:"+File.separator+"test"+File.separator+"test5.txt"; merge(src1,dest1); }运行后结果如下: 三、文件的拆分 如果要实现文件的拆分,前面校验步骤同上,然后 1、提供每一个碎片文件的大小,及需要将目标文件拆分为几个多大的碎片文件 2、分布拆分 具体实现代码如下:
public static void SplitFile(String srcFilePath,int destLength){ //自定义碎片文件的大小 然后将指定文件分割成指定大小 if(srcFilePath==null||destLength==0){ throw new IllegalArgumentException("format must not be null or Length must no be 0"); } File srcFile= new File(srcFilePath); if(!srcFile.exists()&&srcFile.isFile()){ throw new IllegalArgumentException("srcFile is not a file or is not exists"); } int count=0; int len=-1; byte[] data=new byte[destLength]; try(FileInputStream in= new FileInputStream(srcFilePath)) { while((len=in.read(data))!=-1){ File destFile=new File("D:"+File.separator+"test"+File.separator+"test"+(++count)+".txt"+File.separator); FileOutputStream out= new FileOutputStream(destFile); out.write(data); out.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }以上。