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

演示MP3的复制,通过缓冲区。

BufferedOutputStream(OutputStream out)
          创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
BufferedOutputStream(OutputStream out, int size)
          创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。

BufferedInputStream(InputStream in)
          创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
BufferedInputStream(InputStream in, int size)
          创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。


*/
import java.io.*;
class CopyMp3
{
        public static void main(String[] args)
        {
        long start = System.currentTimeMillis();
        copy_1();
        long end = System.currentTimeMillis();
        System.out.println((end-start)+"毫秒");
        }
        public static void copy_1() //throws IOException
        {
                BufferedOutputStream fos=null;
                BufferedInputStream fis=null;
                try
                {
                        fos=new BufferedOutputStream(new FileOutputStream("E:\\jiangbei.ape"));
                        fis=new BufferedInputStream(new FileInputStream("e:\\jiangnan.ape"));
       
                        //byte[] buf=new byte[1024];
                        int len=0;
                        while((len=fis.read())!=-1)
                        {
                                fos.write(len);
                        }   //获取行号和行内容
                }
                catch (IOException e)
                {
                        throw new RuntimeException("复制数据失败");
                }
                finally
                {
                        if(fis!=null)
                        try
                        {
                                fis.close();
                               
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("关闭数据失败");
                        }//抛出new RuntimeException()
                        if(fos!=null)
                        try
                        {
                                fos.close();
                               
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("关闭数据失败");
                        }//抛出new RuntimeException()
                }
        }
}
您需要登录后才可以回帖 登录 | 加入黑马