本帖最后由 007lzg 于 2012-7-27 14:10 编辑
import java.io.*;
class CopyMp3
{
public static void main(String[] args)
{
copy();
}
public static void copy()
{
BufferedInputStream bufis=null;
BufferedOutputStream bufos=null;
try
{
bufis=new BufferedInputStream(new FileInputStream("c:\\0.mp3"));
bufos=new BufferedOutputStream(new FileOutputStream("c:\\1.mp3"));
int ch=0;
while((ch=bufis.read())!=-1)
{
bufos.write(ch);
}
}
catch (IOException e)
{
throw new RuntimeException("复制歌曲失败");
}
finally
{
if(bufis!=null)
try
{
bufis.close();
}
catch (IOException e)
{
throw new RuntimeException("读取歌曲失败");
}
if(bufos!=null)
try
{
bufos.close();
}
catch (IOException e)
{
throw new RuntimeException("写入歌曲失败");
}
}
}
}
===========================================
finally
{
if(bufos!=null)
try
{
bufos.close();
}
catch (IOException e)
{
throw new RuntimeException("读取歌曲失败");
}
if(bufis!=null)
try
{
bufis.close();
}
catch (IOException e)
{
throw new RuntimeException("写入歌曲失败");
}
下面红色部分的代码是正确的,有一点我不明白,到最后关闭bufos.close(); bufis.close();有先后关系吗?
为什么先关闭bufis.close();会出错呢,而且电脑还会嘟嘟的响??????
|
|