完成效果:
将a.mp3和b.mp3分别等分截取成两段partA1、partA2、partB1、partB2,
然后将partA1和partB1、partA2、partB2组合成c.mp3
思路:
获取字节文件长度,然后分成两段
选用字节缓冲流,实现读写
1 package com.heqing.test; 2 3 import java.io.*; 4 5 public class Test3 { 6 public static void main(String[] args) { 7 File fileA = new File("m2/a.mp3"); 8 File fileB = new File("m2/b.mp3"); 9 long lengthA,partA1,partA2,lengthB,partB1,partB2; 10 lengthA = fileA.length(); 11 partA1 = lengthA /2; 12 partA2 = lengthA /2 + lengthA %2; 13 lengthB = fileB.length(); 14 partB1 = lengthB /2; 15 partB2 = lengthB /2 + lengthB %2; 16 try(BufferedInputStream fisa = new BufferedInputStream(new FileInputStream(fileA)); 17 BufferedInputStream fisb = new BufferedInputStream(new FileInputStream(fileB)); 18 BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream("m2/c.mp3"))){ 19 for (int i = 0; i < partA1; i++) { 20 fos.write(fisa.read()); 21 } 22 for (int i = 0; i < partB1; i++) { 23 fos.write(fisb.read()); 24 } 25 for (int i = 0; i < partA2; i++) { 26 fos.write(fisa.read()); 27 } 28 for (int i = 0; i < partB2; i++) { 29 fos.write(fisb.read()); 30 } 31 fos.flush(); 32 } catch (FileNotFoundException e) { 33 e.printStackTrace(); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 } 38 }