字符缓冲流(BufferedReader/BufferedWriter)
字符缓冲流(BufferedReader/BufferedWriter)
可以一次写一行,一次读一行
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("执行完毕"); } }