//仅作为学习笔记
一下操作只适合小的文件 如果大文件 要考虑下内存是否溢出 需要其他处理措施
/*
IO流 切割文件
*/
import java.io.*;
import java.util.*;
class SplitFile
{
public static void main(String []args) throws IOException
{
splitFile();
//merge();
}
//为了合并
public static void merge() throws IOException
{
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for( int x= 1; x <=4; x++)//将流对象添加到集合中
{
al.add(new FileInputStream("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("splitfiles\\0.bmp");
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("1.bmp");
FileOutputStream fos = null;
byte [] buf= new byte[1024*1024];//创建一个缓冲区 此处刚好1M
int len = 0;
int count = 1;
while( (len = fis.read(buf))!= -1)
{
fos = new FileOutputStream("splitfiles\\"+(count++)+".part");
//此处的后缀名是随便创建的 因为一张bmp图片被切割后 已经不完整了 不能在用bmp做后缀
//此处将片段文件都存到 splitfiles 文件下
fos.write(buf,0,len);
fos.close();
}
fis.close();
}
}

如果执行其中的merge() 函数 可以将文件重新合并