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

© 357016138 中级黑马   /  2014-7-23 17:17  /  916 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

编译通过,运行电脑就一直报警,我立刻关机,再开机网也上不去了,系统好像废了

大神们帮我看看,哪里出错了?我去重装系统

  1. import java.io.*;

  2. class Test5
  3. {
  4.         public static void main(String[] args) throws Exception
  5.         {
  6.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("小苹果.mp3"));

  7.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("大苹果.mp3"));

  8.                 byte[] b = new byte[1024];

  9.                 int len = 0 ;

  10.                 while ((len=bis.read(b))!=-1)
  11.                 {
  12.                         System.out.print(new String(b,0,len));
  13.                 }
  14.         }
  15. }
复制代码



2 个回复

倒序浏览
这个代码。。。 怎么直接打印到控制台上了?
看你代码的意思是 将该代码src文件所在的文件夹中的   小苹果.mp3  复制到  该文件夹下 大苹果.mp3

代码问题:
1.  大黄蜂.mp3 可能不存在在硬盘上,需要创建该文件
2.  应该使用输出流写文件 bos.write(f),而不是直接打印到控制台上
3.  应该多加注释,真的,老师也是这么说的

我写的代码
package answer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyMP3 {
       
private static final int SIZE = 1024;

        /* 问题:
* 将 大黄蜂的飞行.mp3文件 复制到 该文件夹下 并改名为 小黄蜂的飞行.mp3
*/
        public static void main(String[] args) throws IOException {
                //1.关联文件,创建字节输入流读取该文件
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\大黄蜂的飞行.mp3"));

                //2.创建目的文件,即小黄蜂的飞行.mp3,并创建字节输出流关联该文件
                File file = new File("E:\\小黄蜂的飞行.mp3");
                if(!file.exists())
                        file.createNewFile();
               
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
               
                //3.定义字节数组缓冲区
                byte[] buf = new byte [SIZE];
               
                //4.读写操作
                int len = 0;
                while((len = bis.read(buf)) != -1){
                        bos.write(buf);
                }
               
                //5.关闭流
                bis.close();
                bos.close();
               
        }

}
回复 使用道具 举报
  1. package answer;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;

  8. public class CopyMP3 {
  9.        
  10. private static final int SIZE = 1024;

  11.         /* 问题:
  12. * 将 大黄蜂的飞行.mp3文件 复制到 该文件夹下 并改名为 小黄蜂的飞行.mp3
  13. */
  14.         public static void main(String[] args) throws IOException {
  15.                 //1.关联文件,创建字节输入流读取该文件
  16.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\大黄蜂的飞行.mp3"));

  17.                 //2.创建目的文件,即小黄蜂的飞行.mp3,并创建字节输出流关联该文件
  18.                 File file = new File("E:\\小黄蜂的飞行.mp3");
  19.                 if(!file.exists())
  20.                         file.createNewFile();
  21.                
  22.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
  23.                
  24.                 //3.定义字节数组缓冲区
  25.                 byte[] buf = new byte [SIZE];
  26.                
  27.                 //4.读写操作
  28.                 int len = 0;
  29.                 while((len = bis.read(buf)) != -1){
  30.                         bos.write(buf);
  31.                 }
  32.                
  33.                 //5.关闭流
  34.                 bis.close();
  35.                 bos.close();
  36.                
  37.         }

  38. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马