Java文件分割
1 package FileDemo; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 public class SpliteFilesDemo { 9 10 private static final int size = 1024*1024; 11 12 /** 13 * @param args 14 * @throws IOException 15 */ 16 public static void main(String[] args) throws IOException { 17 18 File file=new File("D:\\KwDownload\\song\\0.mp3"); 19 splitFile(file); 20 } 21 22 private static void splitFile(File file) throws IOException {// 23 //用读取流关联源文件 24 FileInputStream fis=new FileInputStream(file); 25 //创建一个1M的缓冲区 26 byte buf[]=new byte[size]; 27 //创建目的 28 FileOutputStream fos=null; 29 File destFile=new File("D:\\destFile"); 30 if(!destFile.exists()){ 31 destFile.mkdirs(); 32 } 33 int len=0; 34 int count=1; 35 while((len=fis.read(buf))!=-1){ 36 fos=new FileOutputStream(new File(destFile, (count++)+".part")); 37 fos.write(buf,0,len); 38 } 39 fos.close(); 40 fis.close(); 41 } 42 43 }