当然显示不出来,文件不存在,怎么能显示?
改了一下结果 :读入 2.jpg 生成1.part 还原成1.jpg
package com.ymocean;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 {
public static void main(String[] args) throws IOException {
splitFile();
merge();
}
//将切割的碎片重新变成一个图片
public static void merge() throws IOException{
ArrayList<BufferedInputStream> al=new ArrayList<BufferedInputStream>();
BufferedInputStream bis=null;
int i=1;
while(new File("F:\\MyEclipse\\WorkSpaces\\test\\",i+".part").exists()){//先看是否有文件再写入
FileInputStream fis= new FileInputStream("F:\\MyEclipse\\WorkSpaces\\test\\"+i+".part");
if(fis==null) break;
bis=new BufferedInputStream(fis);
al.add(bis);
i++;
}
final Iterator<BufferedInputStream> it=al.iterator();
Enumeration<BufferedInputStream> en=new Enumeration<BufferedInputStream>() {
public boolean hasMoreElements()
{
return it.hasNext();
}
public BufferedInputStream nextElement()
{
return it.next();
}
};
SequenceInputStream sis=new SequenceInputStream(en);
FileOutputStream fos=new FileOutputStream("1.jpg");
BufferedOutputStream bos=new BufferedOutputStream(fos);
byte [] buf=new byte[1024];
int len=0;
while((len=sis.read(buf))!=-1){
fos.write(buf,0,len);
fos.flush();
}
sis.close();
bos.close();
System.out.println("OK");
}
//将一个文件切割成碎片
public static void splitFile() throws IOException{
FileInputStream fis=new FileInputStream("2.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("F:\\MyEclipse\\WorkSpaces\\test\\"+count+".part");
fos.write(buf,0,len);
System.out.println(count);
count++;
}
fos.close();
fis.close();
System.out.println("OK");
}
}
|