本帖最后由 王金科 于 2012-8-23 15:37 编辑
- 视频中的源代码
- /*
- 文件切割和合并
- */
- import java.util.*;
- import java.io.*;
- class SplitFile
- {
- public static void main(String[] args) throws IOException
- {
- //splitFile();
- merge();
- }
- //合并文件
- public static void merge() throws IOException
- {
- ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
- for(int x=1;x<=6;x++)
- {
- al.add(new FileInputStream("F:\\split\\"+x+".part"));
- }
- final Iterator<FileInputStream> it = al.iterator();
- Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
- {
- public boolean hasMoreElements()
- {
- return it.hasNext();
- }
- public FileInputStream nextElement()
- {
- return it.next();
- }
- };
- SequenceInputStream sis = new SequenceInputStream(en);
- FileOutputStream fos = new FileOutputStream("F:\\2.rm");
- byte[] buf = new byte[1024*1024*20];
- int len = 0;
- while((len=sis.read(buf))!=-1)
- {
- fos.write(buf,0,len);
- }
- fos.close();
- sis.close();
- }
- //切割文件
- public static void splitFile()throws IOException
- {
- FileInputStream fis = new FileInputStream("F:\\1.rm");
- FileOutputStream fos = null;
- byte[] buf = new byte[1024*1024*20];
- int len = 0;
- int count = 1;
- while((len=fis.read(buf))!=-1)
- {
- fos = new FileOutputStream("F:\\split\\"+(count++)+".part");
- fos.write(buf,0,len);
- fos.close();
- }
- fis.close();
- }
- }
复制代码 for(int x=1;x<=6;x++)
{
al.add(new FileInputStream("F:\\split\\"+x+".part"));
}
这里我们是知道碎片文件有6个的情况下才定义x<=6- package cn.study.file;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.SequenceInputStream;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.Iterator;
- public class SplitFile {
- /**
- * @param args
- */
- public static void main(String[] args) throws IOException
- {
- //splitFile();
- merge();
- }
- //合并文件
- public static void merge() throws IOException
- {
- ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
- File dir = new File("F:\\split\\");
- File[] files = dir.listFiles();
- for(int x=1;x<=files.length;x++)
- {
- al.add(new FileInputStream("F:\\split\\"+x+".part"));
- }
- final Iterator<FileInputStream> it = al.iterator();
- Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
- {
- public boolean hasMoreElements()
- {
- return it.hasNext();
- }
- public FileInputStream nextElement()
- {
- return it.next();
- }
- };
- SequenceInputStream sis = new SequenceInputStream(en);
- FileOutputStream fos = new FileOutputStream("F:\\2.rmvb");
- byte[] buf = new byte[1024*1024*20];
- int len = 0;
- while((len=sis.read(buf))!=-1)
- {
- fos.write(buf,0,len);
- }
- fos.close();
- sis.close();
- }
- //切割文件
- public static void splitFile()throws IOException
- {
- FileInputStream fis = new FileInputStream("F:\\1.rmvb");
- FileOutputStream fos = null;
- byte[] buf = new byte[1024*1024*20];
- int len = 0;
- int count = 1;
- while((len=fis.read(buf))!=-1)
- {
- fos = new FileOutputStream("F:\\split\\"+(count++)+".part");
- fos.write(buf,0,len);
- fos.close();
- }
- fis.close();
- }
- }
复制代码 我修改后的代码如下这样子是不是更贴近实际一点?
|