import java.io.*; import java.util.*; class SplitDemo { public static void main(String[] args)throws IOException { splitDemo();//分割文件 sequenceDemo();//合併文件 } public static void sequenceDemo()throws IOException { FileInputStream fis = null; FileOutputStream fos = new FileOutputStream("2.avi"); ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();//Vector效率低 int count = 0; File dir = new File("split");//利用File遍歷文件夾下的文件 File[] files = dir.listFiles(); for(int x=0;x<files.length;x++) { al.add(new FileInputStream(files[x])); } final Iterator<FileInputStream> it = al.iterator();//ArrayList自己沒有枚舉方法,經過迭代器來實現 Enumeration<FileInputStream> en= new Enumeration<FileInputStream>()//匿名內部類,複寫枚舉接口下的兩個方法 { public boolean hasMoreElements(){ return it.hasNext(); } public FileInputStream nextElement() { return it.next(); } }; SequenceInputStream sis = new SequenceInputStream(en); byte[] buf = new byte[1024*1024];定義1M的緩存區 while((count=sis.read(buf))!=-1) { fos.write(buf,0,count); } sis.close(); fos.close(); } public static void splitDemo()throws IOException { FileInputStream fis = new FileInputStream("1.avi"); FileOutputStream fos = null;//要在循環內部建立FileOutputStream對象 byte[] buf = new byte[1024*1024];//將文件分割成1M大小的碎片 int len,count = 0; while((len=fis.read(buf))!=-1) { fos = new FileOutputStream("split\\"+(count++)+".part"); fos.write(buf,0,len); fos.flush(); fos.close(); } fis.close(); } }