1 package om.gh.homework; 2 import java.io.*; 3 /** 4 * 实现分割文件; 5 * @param file 6 */ 7 public class HomeWork { 8 /** 9 * @param src 10 * 要分割的文件路径 11 * @param n 12 * 每个文件的大小以mb为单位 13 * @param desc 14 * 分割的文件存放路径 15 * @throws FileNotFoundException 16 */ 17 public static void filesplit(File src, int mb, File desc) 18 throws FileNotFoundException { 19 // 判断文件路径 20 if (src.exists() && src.isFile() && desc.isDirectory()) { 21 int fileSize = mb * 1024 * 1024; 22 int n; 23 // 判断要分割文件的个数n; 24 if (src.length() % fileSize == 0) 25 n = (int) (src.length() / fileSize); 26 else 27 n = (int) (src.length() / fileSize) + 1; 28 try { 29 InputStream is = new FileInputStream(src); 30 BufferedInputStream bis = new BufferedInputStream(is); 31 for (int i = 0; i < n; i++) {// 循环写入每个文件; 32 byte[] b = new byte[fileSize]; 33 String newfile = desc.getPath() + File.separator 34 + src.getName()+"_"+ i + ".dat"; 35 BufferedOutputStream bos = new BufferedOutputStream(//缓存流 36 new FileOutputStream(newfile)); 37 int len = -1; 38 int count=0; 39 while ((len = bis.read(b)) != -1) { 40 bos.write(b,0,len); 41 count+=len; 42 bos.flush(); 43 if(count>=fileSize)break; 44 } 45 bos.close(); 46 } 47 bis.close(); 48 is.close(); 49 } catch (FileNotFoundException e) { 50 e.printStackTrace(); 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 } else { 55 throw new FileNotFoundException("文件不存在或者参数错误!"); 56 } 57 } 58 59 public static void main(String[] args) { 60 File src = new File("F:\\电影\\万万没想到.mp4");//要分割的目标文件 61 File desc = new File("f:\\视频\\");//分割完存放的路径 62 int mb = 500;// 每个文件大小,以Mb为单位; 63 System.out.println("开始分割..."); 64 try { 65 filesplit(src, mb, desc); 66 } catch (FileNotFoundException e) { 67 e.printStackTrace(); 68 } 69 System.out.println("分割完成!"); 70 } 71 }
1 package om.gh.homework; 2 import java.io.*; 3 /** 4 * 把分割后的文件合并 5 * 6 * @author ganhang 7 * 8 */ 9 public class Homework2 { 10 /** 11 * 12 * @param desc 13 * 合成后的文件路径 14 * @param src 15 * 要合成的文件列表 16 */ 17 public static void merge(File desc, File... src) { 18 try { 19 String filename = src[0].getName().substring(0, 20 src[0].getName().lastIndexOf("_")); 21 File newfile = new File(desc.getPath() + File.separator + filename); 22 OutputStream os = new FileOutputStream(newfile); 23 BufferedOutputStream bos = new BufferedOutputStream(os); 24 for (int i = 0; i < src.length; i++) { 25 BufferedInputStream bis = new BufferedInputStream( 26 new FileInputStream(src[i])); 27 byte[] b = new byte[1024 * 1024]; 28 int len = -1; 29 while ((len = bis.read(b)) != -1) { 30 bos.write(b, 0, len); 31 bos.flush(); 32 } 33 bis.close(); 34 } 35 bos.close(); 36 os.close(); 37 System.out.println("合成成功!"); 38 } catch (FileNotFoundException e) { 39 e.printStackTrace(); 40 } catch (IOException e) { 41 e.printStackTrace(); 42 } 43 } 44 public static void main(String[] args) { 45 File desc=new File("f:\\视频\\"); 46 File [] src={ 47 new File("f:\\视频\\万万没想到.mp4_0.dat"), 48 new File("f:\\视频\\万万没想到.mp4_1.dat"), 49 new File("f:\\视频\\万万没想到.mp4_2.dat"), 50 new File("f:\\视频\\万万没想到.mp4_3.dat") 51 }; 52 System.out.println("开始合成..."); 53 merge(desc,src); 54 } 55 }
时间: 2024-10-23 23:36:06