分割,合并文件
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.Enumeration;
import java.util.Iterator;
import java.util.List;
public class SplitFile {
public static void split() throws Exception {
FileInputStream fis = new FileInputStream("d:\\360\\src.zip");
FileOutputStream fos = null;
byte[] buf = new byte[1024*1024];
int len = 0;
int count = 0;
while ((len = fis.read(buf))!=-1) {
count++;
fos = new FileOutputStream("d:\\360\\"+count+".temp");
fos.write(buf, 0, len);
fos.close();
}
fis.close();
}
public static void merge() throws IOException {
List<FileInputStream> list = new ArrayList<>();
for(int i=1; i<=4; i++){
list.add(new FileInputStream("d:\\360\\"+i+".temp"));
}
final Iterator<FileInputStream> iterator = list.iterator();
Enumeration<FileInputStream> enumeration = new Enumeration<FileInputStream>() {
@Override
public boolean hasMoreElements() {
return iterator.hasNext();
}
@Override
public FileInputStream nextElement() {
return iterator.next();
}
};
SequenceInputStream sis = new SequenceInputStream(enumeration);
FileOutputStream fos = new FileOutputStream("d:\\360\\abc.zip");
byte[] buf = new byte[1024*1024];
int len = 0;
while ((len=sis.read(buf))!=-1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
public static void main(String[] args) throws Exception {
merge();
}
}