码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-11-19 03:32:33      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:redo   字符   imp   api   ;;   highlight   ace   i++   package   

字符缓冲流:  开发时候建议使用缓冲流,速度和效率更快。
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("\n"); } 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();
        }

    }

}

 

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

标签:redo   字符   imp   api   ;;   highlight   ace   i++   package   

原文地址:http://www.cnblogs.com/sunjiqiang/p/6079646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!