// 其代碼漏洞較多,需要知道檔案類型,需要知道所切割檔案的個數。
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;
public class HebingDemo {
public static void main(String[] args) throws IOException {
// TODO 自動生成的方法存根
File start = new File("F:\\BB"); //源檔案的目錄
File end = new File("F:\\AA\\5.png"); //合并後檔案
mergeFile(start,end);
}
public static void mergeFile(File dir,File dir2) throws IOException {
// TODO 自動生成的方法存根
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for (int i = 1; i <7 ; i++) { //根據碎檔案的字尾名而定。
al.add(new FileInputStream(new File(dir,i+".part")));
}
Enumeration<FileInputStream> en = Collections.enumeration(al);
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream(dir2);
byte[] buf = new byte[1024];
int len=0;
while ((len=sis.read(buf))!=-1) {
fos.write(buf, 0, len);
}
sis.close();
fos.close();
}
}