用java实现的文件分割函数(今天刚写的,经测试完全正确),大家可以试一下
//QQ:36748124
//大家共同来探讨 JAVA 的开发
//文件分割操作执行函数
//注意:目标文件名不要带扩展名
public void CutFile(String srcFile,String destFile,int subFileSize)
{
byte[] buff = new byte[1024]; //文件读写缓冲区
File f_src;
int subFileCount=0; //切分成的子文件的数目
f_src=new File(srcFile);
FileInputStream fis;
subFileCount = (int)((f_src.length()%subFileSize==0) ? (f_src.length()/subFileSize) : (f_src.length()/subFileSize)+1);
String [] destFileName=new String[subFileCount];
for(int i=0;i<subFileCount;i++)
{
destFileName[i]=destFile;
destFileName[i] += i;
}
File[] f_dest=new File[subFileCount];
for(int i=0;i<subFileCount;i++)
{
f_dest[i]=new File(destFileName[i]);
}
FileOutputStream [] fos;
try
{
fis=new FileInputStream(f_src);
fos=new FileOutputStream[subFileCount];
for(int i=0;i<subFileCount;i++)
{
fos[i]=new FileOutputStream(f_dest[i]);
}
}
catch(java.io.FileNotFoundException e)
{
System.out.println("java.io.FileNotFoundException");
return;
}
int j=0;
int readNum=0;
int numTotal=0;
try
{
while((readNum=(fis.read(buff,0,buff.length)))!=-1)
{
if(numTotal==subFileSize)
{
j++;
numTotal=0;
}
fos[j].write(buff,0,readNum);
numTotal += readNum;
}
for(j=0;j<subFileCount;j++)
{
if(fos[j]!=null)
fos[j].close();
}
if(fis!=null)
fis.close();
}
catch(IOException e)
{
System.out.println("IOException occured");
return;
}
}