本帖最后由 HKing 于 2014-8-23 19:58 编辑
写一个MP3的复制程序,就当时复习IO流操作吧:D
- /*
- MP3复制
- */
- import java.io.*;
- public class Test {
- public static void main(String[] args) {
-
- //创建目的地
- // FileOutputStream fos = null;
- BufferedOutputStream bos = null;
- //与已有文件关联
- // FileInputStream fis = null;
- BufferedInputStream bis = null;
- //缓冲区
- byte[] bytes = null;
- int len = 0;
- try
- {
- // fos = new FileOutputStream("2.mp3");
- // fis = new FileInputStream("错错错.mp3");
- bis = new BufferedInputStream(new FileInputStream("错错错.mp3"));
- bos = new BufferedOutputStream(new FileOutputStream("2.mp3"));
- bytes = new byte[1024];
- while ((len=bis.read(bytes)) != -1)
- {
- bos.write(bytes, 0, len);
- bos.flush();
- }
- }
- catch (IOException e)
- {
- throw new RuntimeException("复制文件操作失败!");
- }
- finally
- {
- if (bos != null)
- {
- try
- {
- bos.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("关闭资源失败!");
- }
- }
- if (bis != null)
- {
- try
- {
- bis.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("关闭资源失败!");
- }
-
- }
- }
- }
- }
复制代码
注释的地方为不使用缓冲的代码,两种方法可以对比下!
|
|