Java文件分割

时间:2021-11-05 15:26:03
 package FileDemo;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class SpliteFilesDemo { private static final int size = 1024*1024; /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { File file=new File("D:\\KwDownload\\song\\0.mp3");
splitFile(file);
} private static void splitFile(File file) throws IOException {//
//用读取流关联源文件
FileInputStream fis=new FileInputStream(file);
//创建一个1M的缓冲区
byte buf[]=new byte[size];
//创建目的
FileOutputStream fos=null;
File destFile=new File("D:\\destFile");
if(!destFile.exists()){
destFile.mkdirs();
}
int len=0;
int count=1;
while((len=fis.read(buf))!=-1){
fos=new FileOutputStream(new File(destFile, (count++)+".part"));
fos.write(buf,0,len);
}
fos.close();
fis.close();
} }