本帖最后由 祁焱 于 2011-11-24 11:31 编辑
下面代码是照视频上写的,不知道那块写错了,希望大家帮忙解决,代码如下:
import java.io.*;
import java.util.*;
public class SpiltFile {
public static void main(String[] args) throws IOException {
merge();
}
public static void merge() throws IOException {
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for (int x = 1; x <= 4; x++) {
al.add(new FileInputStream("C:\\splitfiles\\" + 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("C:\\0.jpg");
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() throws IOException {
FileInputStream fis = new FileInputStream("C:\\1.jpg");
FileOutputStream fos = null;
byte[] buf = new byte[1024 * 1024];
int len = 0;
int count = 1;
while ((len = fis.read(buf)) != -1) {
fos = new FileOutputStream("C:\\splitfiles\\" + (count++) + ".part");
fos.write(buf, 0, len);
fos.close();
}
fis.close();
}
}
异常信息:
Exception in thread "main" java.io.IOException: 当文件已存在时,无法创建该文件。
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:199)
at java.io.SequenceInputStream.read(SequenceInputStream.java:191)
at java.io.InputStream.read(InputStream.java:85)
at javatest.javaIO3.SpiltFile.merge(SpiltFile.java:39)
at javatest.javaIO3.SpiltFile.main(SpiltFile.java:17)
Java Result: 1
成功生成(总时间:0 秒) |
|