- package FileSplit;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class CopyOfmySplit02 {
- public static void split(String srapath,int blockSize,String destpath) throws IOException{
-
- if((new File(srapath)).isFile()&&new File(destpath).isDirectory()&&(new File(srapath)).lenth>=blockSize){ //判断 srapath 是指向文件,destpath指向目录,并且 文件块的大小<=文件大小
- FileInputStream fis=new FileInputStream(srapath);
- FileOutputStream fos=null;
- byte[]bytes=new byte[blockSize];//通过控制缓冲区的大小实现分割块的大小
- int len=0;
- int count=1;
- while((len=fis.read(bytes))!=-1){
- fos=new FileOutputStream(destpath+"/"+(count++)+".patr");//.part作为后缀.
- fos.write(bytes, 0, len);
- fos.close();
- }
- fis.close();
- }else{
- System.out.println("源文件或者目标目录错误!");
- return;
- }
- }
-
- public static void main(String[] args) throws IOException {
- split("e:/test/1.mp3",1024*1024,"e:/test");//不要太小了,否则会出很多很多个文件!!!
- }
- }
复制代码 |
|