本帖最后由 黑马王鹏 于 2012-8-21 10:48 编辑
- import java.io.*;
- import java.util.*;
- public class splitFile {
- /**
- * @param args
- */
- public static void main(String[] args) throws IOException{
- // TODO Auto-generated method stub
- spiltFile();
- }
- /**
- * 切割文件
- * @throws IOException
- */
-
- public static void spiltFile()throws IOException{
- File avi = new File("C:\\1.avi");
- BufferedInputStream bufi = new BufferedInputStream(new FileInputStream(avi));
- BufferedOutputStream bufo = null;
- File f = new File("C:\\splitfile");
- if(!f.exists())
- f.mkdir();
-
- byte[] buf = new byte[1024*1024];
- int len;
- int count = 1;
- int sum = 0;
- while((len=bufi.read(buf)) != -1){
- //把注释打开,直接切是没问题的
- /* bufo = new BufferedOutputStream(new FileOutputStream(new File(f,count+".part")));
- bufo.write(buf, 0, len);
- bufo.close();
- count++;*/
- if(bufo == null)
- bufo = new BufferedOutputStream(new FileOutputStream(new File(f,count+".part")));
- else{
- //以10M为大小进行切割
- if(sum < 10){
- sum++;
- bufo.write(buf, 0, len);
- }else{
- bufo.close();
- bufo = null;
- sum = 0;
- count++;
- }
- }
- }
- bufo.close();
- bufi.close();
- }
- /**
- * 合并文件
- * @throws IOException
- */
-
- public static void partToAli()throws IOException{
- ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
- for(int i=1; i<44; i++){
- al.add(new FileInputStream(new File("C:\\splitfile\\"+i+".part")));
- }
- final Iterator<FileInputStream> it = al.listIterator();
- Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
- public boolean hasMoreElements(){
- return it.hasNext();
- }
-
- public FileInputStream nextElement(){
- return it.next();
- }
- };
-
- SequenceInputStream sis = new SequenceInputStream(en);
- BufferedOutputStream bufo = new BufferedOutputStream(new FileOutputStream(new File("C:\\splitfile\\0.avi")));
- byte[] buf = new byte[1024*1024];
- int len = 0;
- while((len=sis.read(buf)) != -1){
- bufo.write(buf, 0, len);
- }
- bufo.close();
- sis.close();
- }
- }
复制代码 我要切割一个视频文件,以10M为大小进行切割,我的缓冲区大小是1M,在一个文件中输入10次才行,上面是我的代码,结果切出来的文件和源文件大小不一样,大家看看是什么原因?
我不分开切,以一个文件1M的话切,又没有问题(注释的部分就这样切的)
下面截图是我的运行结果,两种明显不一致
|