字节缓冲流

package FileT.Day3;
/**
 * @author CCQ
 * 字节缓冲流
 *      将要读或者要写的数据放入到缓冲区,然后再一次性读或写。能够提高性能。
 *      BufferedOutputStream();里面共可以有两个参数,一个是原本的字节流对象,一个是int类型的缓冲区长度,不写就是默认大小8192
 *      BufferedInputStream();里面共可以有两个参数,一个是原本的字节流对象,一个是int类型的缓冲区长度,不写就是默认大小8192
 *      用法跟字节流一样
 */

import java.io.*;
import java.util.Arrays;

public class BufferStreamTest {
    public static void main(String[] args) throws IOException {
        BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("fox.txt"));
        bos.write(("I think you will like it!").getBytes());
        BufferedInputStream bis =new BufferedInputStream(new FileInputStream("fox.txt"));
        bos.close();
        //一次读一个字节
        int by;
        while((by =bis.read())!=-1){
            System.out.print((char) by);
        }

        //一次读一组字节
        byte[] b =new byte[1024];
        int len;
        while((len =bis.read(b,0,b.length))!=-1){
            System.out.println(new String(b,0,len));
        }
        bis.close();
    }
}


//复制视频小demo
class demo{
    public static void main(String[] args) throws IOException {
        BufferedInputStream bis =new BufferedInputStream(new FileInputStream("C:\\Users\\CCQ\\Documents\\Tencent Files\\953881082\\FileRecv\\递归.mp4"));
        BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("C:\\Users\\CCQ\\Documents\\Tencent Files\\953881082\\FileRecv\\递归1.mp4"));
        int len;
        byte[] b =new byte[1024];
        while((len =bis.read(b,0,b.length))!=-1){
            bos.write(b,0,len);
        }
        bis.close();
        bos.close();
    }
}

//编码解码小demo
class demo1{
    public static void main(String[] args) throws UnsupportedEncodingException {
        String s ="朱雀";
        byte[] bytes = s.getBytes();
        System.out.println(Arrays.toString(bytes));
        String ss =new String(bytes);
        System.out.println(ss);

        String s1 ="终极迈凯伦";
        byte[] bytes1 = s1.getBytes("GBK");
        System.out.println(Arrays.toString(bytes1));
        String ss2 =new String(bytes1,"GBK");
        System.out.println(ss2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值