一、文件分割测试
-
一切皆为字节:
- testOne() : 是分割方法。
- testTwo() : 是合并方法。
package name.yu.review; import org.junit.Test; import java.io.*; public class Test1 { @Test public void tesTone(){ long starTime = System.currentTimeMillis(); String fileName = "E:\\temp\\"; OutputStream os = null; BufferedOutputStream bos = null; OutputStream os2 = null; BufferedOutputStream bos2 = null; InputStream is = null; BufferedInputStream bis = null; byte[] bytes = new byte[1024]; int len = 0; long sum = 0; try { is = new FileInputStream(fileName + "video.mp4"); bis = new BufferedInputStream(is); os = new FileOutputStream(fileName + "new1.mp4"); bos = new BufferedOutputStream(os); os2 = new FileOutputStream(fileName + "new2.mp4"); bos2 = new BufferedOutputStream(os2); while ((len = bis.read(bytes)) != -1){ if (sum <= 1024*1024*1024){ bos.write(bytes,0,len); }else { bos2.write(bytes,0,len); } sum = sum + len; // 一个 len = 1 kb } } catch (Exception e) { e.printStackTrace(); }finally { if (bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos2 != null){ try { bos2.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (os2 != null){ try { os2.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println(System.currentTimeMillis()-starTime); } @Test public void testTwo(){ long startTime = System.currentTimeMillis(); String fileName = "E:\\temp\\"; InputStream is = null; BufferedInputStream bis = null; InputStream is2 = null; BufferedInputStream bis2 = null; OutputStream os = null; BufferedOutputStream bos = null; int len = 0; byte[] bytes = new byte[1024]; try { is = new FileInputStream(fileName + "new1.mp4"); bis = new BufferedInputStream(is); os = new FileOutputStream(fileName + "new.mp4"); bos = new BufferedOutputStream(os); while ((len = bis.read(bytes)) != -1){ os.write(bytes,0,len); } is2 = new FileInputStream(fileName + "new2.mp4"); bis2 = new BufferedInputStream(is2); while ((len = bis2.read(bytes)) != -1){ os.write(bytes,0,len); } } catch (Exception e) { e.printStackTrace(); }finally { if (bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (bis2 != null){ try { bis2.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (is2 != null){ try { is2.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println(System.currentTimeMillis()-startTime); } }