本帖最后由 刘明瑞 于 2012-1-13 16:44 编辑
我把一个MP3分割成了11个部分,分割部分代码没问题。但是在合并的时候,却发现合并后的大小跟分割前差很远。
请各位大大帮我看一下,代码我是按照毕老师写的代码写的。到底哪里出错了啊?
代码如下:
public class SplitFileDemo {
public static void main(String[] args) throws IOException{
File file_1 = new File("E:\\Mine Mine.mp3");
//splitFile(file_1);
merge(11);
}
public static void merge(int count) throws IOException {
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for(int i = 1; i <= count ; i ++) {
al.add(new FileInputStream("E:\\mine mine\\" +count + ".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("E:\\mine mine\\mine mine.mp3");
byte[] buf = new byte[1024];
int len = 0;
while((len = sis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
sis.close();
}
public static void splitFile(File file_1) throws IOException{
FileInputStream fis = new FileInputStream(file_1);
FileOutputStream fos = null;
byte[] buf = new byte[1024*1024];
int len = 0;
int count = 1;
while((len = fis.read(buf)) != -1) {
fos = new FileOutputStream("E:\\mine mine\\" + (count++) + ".part");
fos.write(buf, 0, len);
fos.close();
}
fis.close();
}
} |