字符缓冲流(BufferedReader/BufferedWriter)

字符缓冲流(BufferedReader/BufferedWriter)

可以一次写一行,一次读一行

支持输入换行符newline();

字符读取缓冲流:
package com.tiedandan.IO流.字符流.字符缓冲流;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

/**
 * 字符读取缓冲流
 */
public class BufferedReaderUse {
    public static void main(String[] args) throws Exception {
        FileReader fr = new FileReader("d://hello.txt");
        BufferedReader br = new BufferedReader(fr);
//        //一般读取
//        char[] byte1 = new char[1024];
//        int count = -1;
//        while ((count=br.read(byte1))!=-1);{
//            System.out.print(new String(byte1,0,count));}
      String line = null;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }

      //关闭
        br.close();
    }
}
字符写入缓冲流:
package com.tiedandan.IO流.字符流.字符缓冲流;

import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriteUse {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("d:\\zhtt.txt");

        BufferedWriter bw = new BufferedWriter(fw);

        for (int i = 0; i <5 ; i++) {
        bw.write("好好学习,天天向上");
        bw.newLine();//添加一个换行符.


        }
        //关闭
        bw.close();
        System.out.println("执行完毕");
    }
}

 

posted on 2021-11-24 11:33  张铁蛋666  阅读(44)  评论(0编辑  收藏  举报

导航