标签:
package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.Properties; public class qiefiledemo { private static int SIZE=1024*1024; /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //文件切割 File file=new File("D:\\3.avi"); splitFile(file); //文件组合 File file2=new File("D:\\cc"); mergeFile(file2); } //文件组合 public static void mergeFile(File dir) throws IOException { //读取配置文件 File[] files =dir.listFiles(new sufFilter(".properties")); //判断文件是否存在 if(files.length!=1) throw new RuntimeException("文件不存在"); //创建文件流 File fl=files[0]; //获取文件信息 Properties prot=new Properties(); FileInputStream fis=new FileInputStream(fl); prot.load(fis); String filename=prot.getProperty("filename"); int count=Integer.parseInt(prot.getProperty("patconut")); //获取目录下所有的碎片文件 File[] patfile=dir.listFiles(new sufFilter(".pat")); if(patfile.length!=count) { throw new RuntimeException("数目不对"); } ArrayList<FileInputStream> al=new ArrayList<FileInputStream>(); for(int i=0;i<patfile.length;i++) { al.add(new FileInputStream(new File(dir,i+".pat"))); } Enumeration<FileInputStream> en=Collections.enumeration(al); SequenceInputStream sis=new SequenceInputStream(en); FileOutputStream fos=new FileOutputStream(new File(dir,filename)); byte[] buf=new byte[SIZE]; int len=0; while((len=sis.read(buf))!=-1) { fos.write(buf,0,len); } fos.close(); sis.close(); } //文件切割 public static void splitFile(File file) throws IOException { //用于读取流的关联文件 FileInputStream fis=new FileInputStream(file); //定义一个1M的缓冲区 byte[] buf=new byte[SIZE]; //创建目地 FileOutputStream fos=null; //创建文件切割配置文件信息 Properties prop=new Properties(); int conut=0; int len=0; File dir=new File("D:\\cc"); if(!dir.exists()) { dir.mkdir(); } while((len=fis.read(buf))!=-1) { fos=new FileOutputStream(new File(dir,(conut++)+".pat")); fos.write(buf,0,len); } //创建配置文件 fos=new FileOutputStream(new File(dir,conut+".properties")); prop.setProperty("patconut",conut+""); prop.setProperty("filename",file.getName()); prop.store(fos,""); fos.close(); fis.close(); } }
package cn.stat.p1.file; import java.io.File; import java.io.FilenameFilter; public class sufFilter implements FilenameFilter { private String suffix; @Override public boolean accept(File dir, String name) { // TODO Auto-generated method stub return name.endsWith(suffix); } public sufFilter(String suffix) { super(); this.suffix = suffix; } }
标签:
原文地址:http://www.cnblogs.com/zywf/p/4782279.html