转载:[url]http://hi.baidu.com/triceratops/blog[/url]
import java.io.*;
public class SplitAndCombineFile{
private byte[] buffer = new byte[1024];
private int current;
public void split(File file, long threshold) throws IOException{
long fileSize = file.length();
int fileFragmentCount = (int)(fileSize/threshold+1);
long accumulate;
String filePath = file.getAbsolutePath();
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos;
for(int i=0;i<fileFragmentCount;i++){
fos = new FileOutputStream(new File(filePath+".part"+(i+1)));
accumulate = 0;
while((current=fis.read(buffer,0,1024))!=-1){
accumulate+=current;
fos.write(buffer,0,current);
fos.flush();
if(accumulate>=threshold)
break;
}
fos.close();
}
}
public void combine(File targetFile, File partFileFolder) throws IOException{
if(partFileFolder.isFile())
throw new IOException("\""+partFileFolder+"\" is not a file folder!");
FileOutputStream fos = new FileOutputStream(targetFile);
FileInputStream fis;
File[] partFiles = partFileFolder.listFiles();
for(int i=0;i<partFiles.length;i++){
fis = new FileInputStream(partFiles[i]);
while((current=fis.read(buffer,0,1024))!=-1){
fos.write(buffer,0,current);
fos.flush();
}
}
fos.close();
}
}