处理流是包在别的流上面的流,相当于包在别的管道上面的管道。(节点流是直接怼在数据源上的流)
缓冲流:缓冲流相当于自带小桶,对读写数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。BufferedReader提供了一个一次读取一行的方法 readLine(),BufferedWriter提供了写入一个行分隔符的方法 newLine()
BufferedReader(Reader in) (Reader in ,int size)
BufferedWriter(Writer out) (Writer out,int size)
BufferedInputStream(InputStream in) (InputStream in,int size)
BufferedOutputStream(OutputStream out) (OutputStream out,int size)
操作实例:
import java.io.*; public class Test { public static void main(String[] args) { //写入100个数对应的字符 try { BufferedWriter bwout = new BufferedWriter(new FileWriter("e:\\java\\aaa.txt")); int sum = 0; for(int i = 32;i <= 99;i++) { //bwout.write("(" + Integer.toString(i) + ")"); bwout.write(i);//实际上写进去的是一个个字符 //bwout.write(" ");//32是一个空格符 sum++; if (sum == 10) { bwout.newLine();//BufferedWriter的newLine()方法 sum = 0; } } bwout.flush();//要flush一次才能将缓冲区输出 bwout.close(); //读取每一行并打印在屏幕上 BufferedReader bread = new BufferedReader(new FileReader("e:\\java\\aaa.txt")); System.out.println(bread.read()); bread.mark(500);//为什么mark(2)与mark(5)mark(5)运行效果是一样的?里面填的数值没有影响? for (int j = 0;j <= 4;j++) { System.out.print(bread.read() + " "); } System.out.println(" "); bread.reset(); for (int j = 0;j <= 4;j++) { System.out.print(bread.read() + " "); } System.out.println(" "); System.out.println(bread.readLine());//从当前位置开始读到此句尾 System.out.println(bread.readLine());//一行一行的读数据,从当前位置开始读到此句尾 bread.close(); }catch(IOException e){ e.printStackTrace(); } } }
时间: 2024-10-11 18:02:09