• IO流(二)__BufferedReader和BufferedWriter


    BufferedReader和BufferedWriter

    字符流的缓冲区:缓冲区的而出现提高了对数据的读写效率
    对应类:BufferedWriter  BufferedReader
    缓冲区要结合流才可以使用,在流的基础上对流的功能进行了增强。实际上BufferedReader和BufferedWriter是对FileReader和FileWriter的装饰模式
    BufferedWriter:

    bufw.newLine();

    public class BufferedWriterDemo {
    
        private static final String LINE_SEPARATOR =System.getProperty("line.separator");
    
        public static void main(String[] args) throws IOException {
    
            FileWriter fw= new FileWriter("buf.txt");
            //为了提高效率。使用字符流的缓冲区
            //创建了一个字符写入流的缓冲区对象,并和指定缓冲流对象相关联
            BufferedWriter bufw=new BufferedWriter(fw);
            //使用缓冲区的写入方法将数据先写入缓冲区中        
            //bufw.write("afdafdsdgg"+LINE_SEPARATOR+"fggag");
            bufw.write("xifasf2");
            bufw.newLine();
            bufw.write("xifasf");
            for (int x = 0; x <4; x++) {
                bufw.write("xifasf");
                bufw.newLine();        
            }
            //使用缓冲区的刷新方法将数据刷到目的地中
            //bufw.flush();
            //关闭缓冲区。其实关闭的就是缓冲的流对象
            bufw.close();       
        }
    }

    BufferedReader:

    line=buf.readLine()

    public class BufferedReaderDemo {
        public static void main(String[] args) throws IOException {
        FileReader fr= new FileReader("buf.txt");
        BufferedReader buf=new BufferedReader(fr);
        String line=null;
        while((line=buf.readLine())!=null){
            System.out.println(line);
        }
        buf.close();
    }

    练习: 自定义的读取缓冲区。其实就是模拟BufferedReader。

    分析:
    * 缓冲区中无非就是封装了一个数组
    * 并对完提供了更多的方法对数组进行访问
    * 其实这些方法最终操作的都是数组的角标
    * 缓冲的原理:
    * 其实就是从源中获取一批数据装进缓冲区中
    * 在从缓冲区中不断取出一个一个数据
    * 在此次取完后,再从源中继续取一批数据进缓冲区
    * 当源中的数据取光时,用-1作为结束标记

    public class MyBufferedReader {
        private FileReader r;
        //定义一个数组作为缓冲区
        private char[] buf=new char[1024];
        //定义一个指针用于操作这个数组中的元素。当操作到最后一个元素后,指针归0;
        private int pos=0;
        //定义一个计数器用于记录缓冲区的数据个数。当数据减到0,就从源中继续读取数据到缓冲区中。
        private int count=0;
        
        MyBufferedReader(FileReader r){
            this.r=r;
        }
        
        public int myRead() throws IOException{
            if(count==0){
                count=r.read(buf);
                pos=0;                                        
            }
            if(count<0){
                return -1;
            }
            char ch=buf[pos++];
            count--;
            return ch;
            
        }
        
        
    public String myReadLine() throws IOException{
        StringBuilder sb=new StringBuilder();
        int ch=0;
        while ((ch=myRead())!=-1){
            if(ch=='
    ')
                continue;
            if(ch=='
    ')
                return sb.toString();
            //将缓冲区中读到的字符,存储到缓存行数据的缓存区中。
            sb.append(ch);
        }
        if(sb.length()!=0){
            return sb.toString();
        }
        return null;    
        }
                        
    }
  • 相关阅读:
    Linux从程序到进程
    Linux用户与“最小权限”原则
    Linux进程关系
    Linux信号基础
    Linux进程基础
    Sublime Text 报“Pylinter could not automatically determined the path to lint.py
    Linux文本流
    Linux文件管理相关命令
    Linux命令行与命令
    Linux架构
  • 原文地址:https://www.cnblogs.com/xiangkejin/p/5935177.html
Copyright © 2020-2023  润新知