zoukankan      html  css  js  c++  java
  • java基础知识回顾之javaIO类---BufferedReader和BufferedWriter

    使用了装饰设计模式:此类的设计是为了提高流操作数据的效率。思想就是定义容器将数据进行临时存储,对于缓冲区对象,其实就是将这个容器进行了分装,并提供了更高效的操作方法。

    BufferReader:
    package com.lp.ecjtu;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class BufferReaderDemo {
    
        /**
         * @param args
         * @throws IOException
         * 
         * 该缓冲去提供了一个的读取一行  readLine()方法,当返回null时读到末尾
         */
        public static void main(String[] args) throws IOException {
            //创建一个读取流对象和文件相关联
            FileReader fr = new FileReader("FileReaderDemo.java");
            //为了提高字符读取的效率,加入缓冲技术
            //将需要被提高效率的流对象作为参数传入缓冲区的构造方法即可
            BufferedReader bw = new BufferedReader(fr);
            String line = null;
            /*line = bw.readLine();
            System.out.println("******"+line);//一次性读取一行
            String line1 = bw.readLine();
            System.out.println("******"+line1);
            String line2 = bw.readLine();
            System.out.println("******"+line2);*/
            while((line=bw.readLine()) != null){
                System.out.println(line);
            }
            bw.close();
            
        }
    
    }

    BufferWriter:

    package com.lp.ecjtu;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class BufferWriterDemo {
    
        /**
         * @param args
         * @throws IOException
         * 
         * 该缓冲去提供了一个跨平台的换行符  newLine方法
         */
        public static void main(String[] args) throws IOException {
            //创建一个字符写入流对象
            FileWriter fw = new FileWriter("buf.txt");
            //为了提高字符写入的效率,加入缓冲技术
            //将需要被提高效率的流对象作为参数传入缓冲区的构造方法即可
            BufferedWriter bw = new BufferedWriter(fw);
            for(int i=0;i<5;i++){
                bw.write("abcd"+i);
                bw.newLine();
                bw.flush();
            }
        }
    }

    通过缓冲区将一个文本的内容复制到另一个文件当中:

    package com.lp.ecjtu;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class CopyTextByBuffer {
        
        /**
         * @param args
         * readLine()方法返回回车符之前数据的内容,并不返回回车符,可以通过newLine()方法换行
         */
        public static void main(String[] args) {
            BufferedReader bufr = null;
            BufferedWriter bufw = null;
            try {
                 bufr = new BufferedReader(new FileReader("FileReaderDemo.java"));
                 bufw = new BufferedWriter(new FileWriter("bufferWriterCopy.txt"));
                 String line = null;
                 while((line=bufr.readLine())!=null){
                     bufw.write(line);
                     bufw.newLine();
                     bufw.flush();
                 }
            } catch (FileNotFoundException e) {
                throw new RuntimeException("没有找到指定文件!请确认文件名称是否正确!");
            }catch (IOException e) {
                throw new RuntimeException("读写失败!");
            }finally{
                try {
                    if(bufr != null){
                        bufr.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException("读取关闭失败!");
                }
                try {
                    if(bufw != null){
                        bufw.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException("写入关闭失败!");
                }
            }
        }
    }

     原理;
    1,使用流的read方法从源中读取一批数据存储到缓冲区的数组中。
    2,通过计数器记录住存储的元素个数。
    3,通过数组的角标来获取数组中的元素(从缓冲区中取数据).
    4,指针会不断的自增,当增到数组长度,会归0.计数器会自减,当减到0时,就在从源拿一批数据进缓冲区。

    package com.lp.ecjtu;
    
    import java.io.FileReader;
    import java.io.IOException;
    
    class MybufferReader {
        private FileReader r;
        public MybufferReader(FileReader r){
            this.r = r;
        }
        //可以一次性读一行数据的方法
        public String myreadLine() throws IOException{
            StringBuilder sb = new StringBuilder();
            int ch = 0;
            while((ch = r.read()) != -1){
                if(ch == '
    '){
                    continue;
                }
                if(ch == '
    '){
                    return sb.toString();
                }else{
                     sb.append((char)ch);
                }
            }
            if(sb.length() != 0){
                return sb.toString();
            }
            return null;
        }
        public void myClose() throws IOException{
            r.close();
        }
    
    }
     public class MybufferReaderDemo{
        public static void main(String[] args) throws IOException{
            FileReader fr = new FileReader("buf.txt");
            MybufferReader mybuffer =  new MybufferReader(fr);
            String line = null;
            while((line =mybuffer.myreadLine())!= null){
                System.out.println(line);
            }
            mybuffer.myClose();
        }
    }
     
  • 相关阅读:
    原创【cocos2d-x】CCMenuItemToggle 在lua中的使用
    SQL Server之LEFT JOIN、RIGHT LOIN、INNER JOIN的区别
    VS的IISExpress配置通过IP访问程序
    SQLServer执行大脚本文件时,提示“无法执行脚本没有足够的内存继续执行程序 (mscorlib)”
    jqGrid中multiselect: true 操作checkbox
    display:table的几个用法(元素平分宽度,垂直居中)
    ASP.NET中 前后台方法的相互调用
    AspxGridView使用手记
    大量文本框非空判断,如何提高灵活性?
    Mysql安装、配置、优化
  • 原文地址:https://www.cnblogs.com/200911/p/3955038.html
Copyright © 2011-2022 走看看