A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 yjsf216 于 2015-3-8 01:43 编辑
  1. public class copyMP3{
  2.         public static void main(String[] args){
  3.                 BufferedInputStream bufis = null;
  4.                 BufferedOutputStream bufos = null;
  5.                 try{
  6.                         bufis= new BufferedInputStream(new FileInputStream("a.mp3"));
  7.                         bufos= new BufferedOutputStream(new FileOutputStream("b.mp3"));
  8.                         
  9.                         byte[] buf= new byte[1024*5];
  10.                         int len= 0;
  11.                         
  12.                         while((len=bufis.read(buf))!=-1){
  13.                                 bufos.write(buf);
  14.                         }
  15.                 }
  16.                 catch(IOException e){
  17.                         System.out.println("dqcw");
  18.                 }
  19.                 finally{
  20.                         try{
  21.                                 bufis.close();
  22.                                 bufos.close();
  23.                         }
  24.                         catch(IOException e){
  25.                         }        
  26.                 }
  27.         }
  28. }
复制代码

为什么我复制一个MP3后的比原来打了,但是播放都一样啊?

3 个回复

正序浏览
赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞
回复 使用道具 举报
jamesli 发表于 2015-3-7 23:29
如果内容不是正好符合字节数组buf的长度的整数倍,那么最后一次读取len个字节到字节数组时,但len个字节后 ...

受教 了,欠考虑
回复 使用道具 举报
如果内容不是正好符合字节数组buf的长度的整数倍,那么最后一次读取len个字节到字节数组时,但len个字节后面,仍然有上次读取的字节未覆盖,而第13行  bufos.write(buf);执行的是写入整个字节数组内容,因此,最后一次未覆盖的内容也写进去了。第13行应改为:bufos.write(buf,0,len);
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马