import java.io.*;
import java.util.*;
class Spiltfiledemo
{
public static void main(String[] args) throws IOException
{
//spiltfile();
merge();
}
//用合并流合并数据
public static void merge() throws IOException
{
ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
for (int x=1;x<=3 ;x++ )
{
al.add(new FileInputStream("d:\\spilt\\"+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("d:\\spilt\\1.bmp");//目的文件
byte[] b=new byte[1024*10];
int len=0;
while ((len=sis.read(b))!=-1)
{
fos.write(b,0,len);
}
fos.close();
sis.close();
}
public static void spiltfile()throws IOException
{
FileInputStream fis=new FileInputStream("d:\\2.jpg");
FileOutputStream fos=null;
byte[] b=new byte[1024*300];//第一个字节缓冲区
int len=0;
int count=1;
while ((len=fis.read(b))!=-1)//将数据读取到byte中
{
fos=new FileOutputStream("d:\\spilt\\"+(count++)+".part");//将切割的碎片出入到spilt文件中
fos.write(b,0,len);
fos.close();
}
fis.close();
}
}
|