/*文件切割
*/
import java.io.*;
class SplitFile
{
public static void main(String[] args) throws IOException
{
File file = new File("f:\\1.bmp");
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[1024*1024];
int count = 1;
int len = 0;
FileOutputStream fos = null;
while((len=fis.read(buf))!=-1)
{
fos = new FileOutputStream("f:\\splitfile\\"+(count++)+".part");
fos.write(buf,0,len);
fos.close();
}
String info = file.getName()+">"+(count-1);
fos = new FileOutputStream("f:\\splitfile\\"+(count++)+".partconfig");
fos.write(info.getBytes());
fos.close();
System.out.println(count);
fis.close();
}
}
/*文件合并
*/import java.io.*;
import java.util.*;
class HeBingFile
{
public static void main(String[] args) throws IOException
{
File f = new File("f:\\splitfile");
File[] files = f.listFiles();
File config = null;
for(File file : files)
{
if(file.getName().endsWith(".hahaconfig"))
{
config = file;
break;
}
}
if(config==null)
{
System.out.println("配置文件没有下载成功");
return ;
}
BufferedReader bufr = new BufferedReader(new FileReader(config));
String info = bufr.readLine();
String[] infos = info.split(">");
String name = infos[0];
int count = Integer.parseInt(infos[1]);
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for(int x=1; x<=count; x++)
{
File ff = new File("f:\\splitfile\\"+x+".haha");
if(!ff.exists())
{
System.out.println(ff.getName()+"..该文件不存在");
return;
}
al.add(new FileInputStream(ff));
}
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("F:\\splitfile\\"+name);
byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
}