本帖最后由 杜光 于 2013-6-9 22:39 编辑
下面这段代码中,被拆后 重新组合的文件总是和源文件大小不一致,求解~~~!!!- import java.util.*;
- import java.io.*;
- class SplitFile
- {
- public static void main(String[] args)throws IOException
- {
- splitFile();
- sequenceFile();
- }
- public static void splitFile()throws IOException
- {
- FileInputStream fis = new FileInputStream("d:\\Demo\\day20\\1.mp3");
- FileOutputStream fos = null;
- byte[] buf = new byte[1024*1024];
-
- int count = 1;
- int len = 0;
- while((len=fis.read(buf))!=-1)
- {
- fos = new FileOutputStream("d:\\Demo\\day20\\splitfiles\\"+(count++)+".part");
- fos.write(buf,0,len);
- fos.close();
- }
- fis.close();
- }
- public static void sequenceFile()throws IOException
- {
- Vector<FileInputStream> v = new Vector<FileInputStream>();
- for (int a=1; a<6; a++)
- {
- v.add(new FileInputStream("D:\\Demo\\day20\\splitfiles\\"+(a++)+".part"));
- }
- Enumeration<FileInputStream> en = v.elements();
- SequenceInputStream sis = new SequenceInputStream(en);
- FileOutputStream fos = new FileOutputStream("d:\\1.mp3");
- byte[] buf = new byte[1024];
- int len = 0;
- while((len=sis.read(buf))!=-1)
- {
- fos.write(buf,0,len);
- }
- fos.close();
- sis.close();
- }
- }
复制代码 |