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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 终结者 中级黑马   /  2013-11-1 16:09  /  1208 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.*;
class CopyMp3Test
{
public static void main(String[] args) throws IOException
{
  long start=System.currentTimeMillis();
  //copy();
  //copy1();
  copy3();
  //copy2();
  long end=System.currentTimeMillis();
  System.out.println(end-start+"毫秒");
}
public static void  copy() throws IOException{     //为什么用下面方法虽然能播放但是在播放时歌曲名字却出了问题(字节没变少),播放的歌曲时间显示不了
FileInputStream in=new FileInputStream("3.mp3");
  FileOutputStream out=new FileOutputStream("5.mp3");
  byte[] buf=new byte[1024];
  int len=0;
  while((len=in.read(buf))!=-1){
   out.write(buf,0,len);
  }
  in.close();
  out.close();
}
public static void  copy3() throws IOException{     //为什么用下面方法复制不了歌曲
FileInputStream in=new FileInputStream("3.mp3");
  FileOutputStream out=new FileOutputStream("9.mp3");
  int len=0;
  while((len=in.read())!=-1){
   out.write(len);
  }
  in.close();
  out.close();
}
public static void copy1() throws IOException{       //虽然复制速度快了,但是为什么在没有关闭资源的情况下,歌曲的字节变少了却能播放而上面的方法在没有关闭资源的情况下字节却没变少
  BufferedInputStream in=new BufferedInputStream(new FileInputStream("3.mp3"));
  BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("6.mp3"));
  byte[] buf=new byte[1024];
  int len=0;
  while((len=in.read(buf))!=-1){
   out.write(buf,0,len);
  }
  in.close();
  out.close();
}
public static void copy2() throws IOException{      
   BufferedInputStream in=new BufferedInputStream(new FileInputStream("3.mp3"));
   BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("7.mp3"));
   int len=0;
   while((len=in.read())!=-1){
    out.write(len);
   }
   in.close();
   out.close();
}
}

评分

参与人数 1技术分 +1 收起 理由
狼王 + 1 赞一个!

查看全部评分

5 个回复

倒序浏览
  1. public class CopyMusic {

  2. public static void main(String[] args) throws Exception {
  3. // copy();
  4. // copy2();
  5. copy3();

  6. }

  7. public static void copy() throws Exception {
  8. // 我复制的是一个1.5m的MP3的歌 这个方法太慢了 得30-60秒
  9. FileInputStream fis = new FileInputStream(new File("d:\\star.mp3"));
  10. FileOutputStream fos = new FileOutputStream(new File("d:\\copy.mp3"));

  11. int len = 0;

  12. while ((len = fis.read()) != -1) {
  13. fos.write(len);
  14. }
  15. fos.close();
  16. fis.close();

  17. }

  18. public static void copy2() throws Exception {
  19. // 这个方法还是非常快的 3秒

  20. FileInputStream fis = new FileInputStream(new File("d:\\star.mp3"));
  21. FileOutputStream fos = new FileOutputStream(new File("d:\\copy2.mp3"));
  22. byte[] buf = new byte[1024];
  23. int len = 0;
  24. while ((len = fis.read(buf)) != -1) {
  25. fos.write(buf, 0, len);
  26. }
  27. fos.close();
  28. fis.close();

  29. }

  30. public static void copy3() throws Exception {

  31. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("d:\\star.mp3")));
  32. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("d:\\copy3.mp3")));

  33. int len = 0;
  34. while ((len = bis.read()) != -1) {
  35. bos.write(len);
  36. }

  37. bos.close();
  38. bis.close();

  39. }

  40. }
复制代码
我的3个copy都正确  就是第一个太慢了
而且字节都不少  都能正常播放 !!!!!!!!!!!






评分

参与人数 1技术分 +1 收起 理由
狼王 + 1 赞一个!

查看全部评分

回复 使用道具 举报
你还是没解决我上面的疑问,我问的是为什么,不是怎么做,我要的答案是原理
回复 使用道具 举报
加油哈,好好努力,为了黑马
回复 使用道具 举报
我来回答你的第三个问题,close的作用是关闭此文件输出流并释放与此流有关的所有系统资源。也就是你还有一部分资源在流中没刷出来!必须close

点评

FFF
要是有例子神马的,技术分就是你的了!  发表于 2013-11-17 10:37
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马