黑马程序员之IO字节流及缓冲技术

字节流

输入流InputStream
输出流OutputStream
字节流相对于字符流主要处理字节数据,图片、音乐等。

public static void main(String[] args)   
    {  
        FileOutputStream fo = null;// 
        FileInputStream fi = null;  
        try  
        {  
            fos = new FileOutputStream("c:\\2.bmp");//写入  
            fis = new FileInputStream("c:\\1.bmp");//读取  
            byte[] buf = new byte[1024];  
            int len =0;  
            while((len=fis.read())!=-1)  
            {  
                fos.write(buf,0,len);  
            }  
        }  
        catch(IOException e)  
        {  
            throw new RuntimeException("复制文件失败");  
        }  
        finally  
        {  
            try  
            {  
                fis.close();  
            }  
            catch(IOException e)  
            {  
                throw new RuntimeException("读取关闭失败");  
            }  
            try  
            {  
                fos.close();  
            }  
            catch(IOException e)  
            {  
                throw new RuntimeException("写入关闭失败");  
            }  
        }  
    }   

字节流缓冲技术

BufferedInputStream类和BufferedOutputStream类

public static void main(String[] args) throws IOException  
    {  
        copy_1();  
    }  
    public static void copy_1() throws IOException  //抛异常
    {  
        BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("c:\\0.mp3"));  //与字符流的逻辑一致
        BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("c:\\1.mp3"));  
        int by =0;  
        while((by=bufis.read())!=-1)  
        {  
            bufos.write(by);  
        }  
        bufos.close();  
        bufis.close();  
    }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值