java文件分割代码实现

来源:互联网 发布:怎么拍下淘宝里的货 编辑:程序博客网 时间:2024/05/19 15:43
public static void qieGe(int a,String srcPath,String desPath){File srPath = new File(srcPath);//获得分割文件long size = srPath.length();//获得文件大小long b = size/a;//每一份分割文件大小String lastName = srcPath.substring(srcPath.lastIndexOf("."));//截取后面的文件后缀String subName =srcPath.substring(0,srcPath.lastIndexOf("."));//截取文件的前缀BufferedInputStream bu = null;  BufferedOutputStream ty = null;try {bu = new BufferedInputStream(new FileInputStream(srPath));int res =0;int sizeCount = 0;for(int i = 0;i<a;i++){ty = new BufferedOutputStream(new FileOutputStream(new File(subName+i+lastName)));byte[] b1 =new byte[1024*8];while((res = bu.read(b1))!=-1){ty.write(b1,0,res);sizeCount += res;if(sizeCount>=b){System.out.println(sizeCount);break;}ty.flush();}}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {bu.close();ty.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println("截取完成!");}

原创粉丝点击