java IO文件的切割和合并。
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/6/12.
*/
public class Demo {
//文件分割
public static List<String> cut(String src,int cutCount){
List<String> nameList = new ArrayList<String>();
File file = new File(src);
byte[] buf = new byte[(int)file.length() / cutCount+1];//加1的原因是有时候除不尽有余数。
InputStream is = null;
BufferedInputStream bis = null;
try {
is = new FileInputStream(file);
bis = new BufferedInputStream(is);
int len = -1;
int number = 1;
while ((len=bis.read(buf))!=-1){
String smallFileName = "e:/" + file.getName().substring(0, file.getName().indexOf(".")) + "-" + number + ".bak";
OutputStream os = new FileOutputStream(smallFileName);
nameList.add(smallFileName);
BufferedOutputStream bos = new BufferedOutputStream(os);
bos.write(buf,0,len);
bos.close();
os.close();
number++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return nameList;
}
//文件合并
public static void merge(List<String> list, String newName) {
OutputStream os = null;
BufferedOutputStream bos = null;
try {
os = new FileOutputStream(newName);
bos = new BufferedOutputStream(os);
int count = list.size();
for(int i=0;i<count;i++){
File file = new File(list.get(i));
byte[] buf = new byte[(int) file.length()];
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
bis.read(buf);
bos.write(buf);
bis.close();
is.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bos.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
String str = "E:/英雄时刻/英雄时刻_20160604-00点11分36s.avi";
List<String> nameList=cut(str,3);
merge(nameList,"E:/精彩视频.avi");
}
}