• 缓冲输入输出流[也叫包装流] BufferedInputStream,BufferedOutputStream,BufferedReader和BufferedWriter


    字符缓冲流:  开发时候建议使用缓冲流,速度和效率更快。
    BufferedReader的api部分:
    BufferedInputStream 部分api: 可以看出
    BufferedInputStream是没有readLine()方法。更多具体,请自行查看api文档


    package cn.bean.demo.io.buffer; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; public class BufferedReaderTest { public static void main(String[] args) { testReadWrite(); } static void testReadWrite(){ try ( Reader read = new FileReader("String1.txt"); BufferedReader buff = new BufferedReader(read); Writer writer = new FileWriter("String2.txt"); BufferedWriter buffWrite = new BufferedWriter(writer); ) { //每次读取的数据放入到缓冲区 char[] ch=new char[10]; // 每次读取的时候放入到缓冲区的实际字符个数 int len=0; while((len=buff.read(ch))!=-1){ buffWrite.write(ch, 0, len);; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } static void testReadWriteLine(){ try ( Reader read = new FileReader("String1.txt"); BufferedReader buff = new BufferedReader(read); Writer writer = new FileWriter("String2.txt"); BufferedWriter buffWrite = new BufferedWriter(writer); ) { String content=null; int i=0; while((content=buff.readLine())!=null){ if(i!=0){ //第一次不换行,当第二次写的时候换行 buffWrite.write(" "); } buffWrite.write(content); i++; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

      再看一下字符缓冲流:

    package cn.bean.demo.io.buffer;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class BufferedInputStreamTest {
    
        public static void main(String[] args) {
            testReadWriteReadLine();
        }
    
        static void testReadWriteReadLine() {
    
            try (InputStream in = new FileInputStream("String1.txt");
                    BufferedInputStream buffer = new BufferedInputStream(in);
                    OutputStream out = new FileOutputStream("String2.txt");
                    BufferedOutputStream bufferWrite = new BufferedOutputStream(out);) {
                    //BufferedInputStream没有readLine方法
                     //创建一个用来存放每次读取的数据的缓冲区
                     byte[] b=new byte[20];
                      //每次读取,并放入到缓冲区的实际字符个数
                     int len=0;
                     while((len=buffer.read(b))!=-1){
                         bufferWrite.write(b, 0, len);
                     }
                
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
  • 相关阅读:
    必须掌握的八个DOS命令
    实况足球8 功略简解
    开始→运行→命令集锦
    必须掌握的八个DOS命令
    对称加密算法之DES算法
    让你的Linux像黑客帝国的画面一样炫酷
    古典密码之凯撒密码and换位密码
    用eclipse写jsp报以下错误
    mysql安装后,过一段时间,在命令行无法启动
    sql 日期函数
  • 原文地址:https://www.cnblogs.com/sunjiqiang/p/6079646.html
Copyright © 2020-2023  润新知