字节,字符缓冲流读写数据(IO流)

目录

1.字节缓冲流读写数据

2.字符缓冲流读写数据

1.字节缓冲流读写数据

import java.io.*;

//字节缓冲流读写数据
public class demo03 {
    public static void main(String[] args) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test\\bbb.txt"));
        bos.write(98); //write(int b)  写入一个字符 b
        bos.write(97); //write(int b)  写入一个字符 a
        bos.write("qweqwe".getBytes(),0,4);  //write(byte[] b,int off,int len) 写入字节数组索引0-4的的值
        bos.close();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test\\bbb.txt"));
        byte[] b = new byte[1024];
        int len =0;
        while((len=bis.read(b))!=-1){
            System.out.println(new String(b,0,len));//打印读的字节数组
        }
        //关闭字节缓冲流,释放空间
        bis.close();
     }
}

2.字符缓冲流读写数据

import java.io.*;

//字符缓冲流读写数据
public class demo04 {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("test\\ccc.txt"));
        bw.write("rtyyhrtgeg",0,6); //write(String s,int off, int len)
        bw.write("\r\n");
        char[] ch = new char[]{'q','w','q'};
        bw.write(ch,0,2); // write(char[] ch,int off, int len);
        bw.close();
        BufferedReader br = new BufferedReader(new FileReader("test\\ccc.txt"));
        String line =null;
        while((line=br.readLine())!=null){ //读一行数据,不包括回车换行符
            System.out.println(line);
        }
        //关闭字符缓冲流,释放空间
        
        br.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值