本帖最后由 格子、 于 2014-5-31 14:32 编辑
今天学到了IO的字节流,做一个复制MP3的例子,代码没错,但是抛出了RuntimeException异常:复制文件读写失败,这是什么情况?有木有遇到这种情况的,求解释?- import java.io.*;
- class CopyMp3Demo
- {
- public static void main(String[] args)
- {
- //System.out.println("Hello World!");
- copyMp3();
- }
- public static void copyMp3()
- {
- BufferedInputStream bis = null;
- BufferedOutputStream bop = null;
- try
- {
- bis = new BufferedInputStream(new FileInputStream("c:\\1.mp3 "));
- bop = new BufferedOutputStream(new FileOutputStream("c:\\2.mp3 "));
- byte[] by = new byte[1024];
- int len = 0;
- while((len=bis.read(by))!=-1)
- bop.write(by,0,len);
- }
- catch (IOException e)
- {
- throw new RuntimeException("复制文件读写失败");
- }
- finally
- {
- if(bis!=null)
- try
- {
-
- //关闭资源
- bis.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("复制文件读取关闭失败");
- }
- if(bop!=null)
- try
- {
-
- //关闭资源
- bop.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("复制文件关闭关闭失败");
- }
- }
- }
- }
复制代码
|