14°

Java基础IO类之缓冲流

首先要明确一个概念:

  对文件或其他目标频繁的读写操作,效率低,性能差。

  使用缓冲流的好处是:能够高效的读写信息,原理是先将数据先缓冲起来,然后一起写入或者读取出来。

对于字节:

BufferedInputStream:为另一个输入流添加一些功能,在创建BufferedInputStream时,会创建一个内部缓冲区数组,用于缓冲数据。

BufferedOutputStream:通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。

对于字符:

BufferedReader:将字符输入流中读取文本,并缓冲各个字符,从而实现字符、数组、和行的高效读取。

BufferedWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

 

代码示例:

package IODemo;

import java.io.; / 缓冲的目的: 解决在写入文件操作时,频繁的操作文件所带来的性能降低的问题 BufferedOutStream 内部默认的缓冲大小是 8Kb,每次写入储存到缓存中的byte数组中,当数组存满是,会把数组中的数据写入文件 并且缓存下标归零 */ public class BufferedDemo {

</span><span style="color: #008000;">//</span><span style="color: #008000;">使用新语法 会在try里面帮关掉这个流</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> BuffRead2(){
    File file </span>= <span style="color: #0000ff;">new</span> File("d:\\test\\t.txt"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">try</span> (BufferedInputStream bis = <span style="color: #0000ff;">new</span> BufferedInputStream(<span style="color: #0000ff;">new</span><span style="color: #000000;"> FileInputStream(file))){

        </span><span style="color: #0000ff;">byte</span>[] bytes = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[1024<span style="color: #000000;">];
        </span><span style="color: #0000ff;">int</span> len = -1<span style="color: #000000;">;
        </span><span style="color: #0000ff;">while</span> ((len = bis.read(bytes))!=-1<span style="color: #000000;">){
            System.out.println(</span><span style="color: #0000ff;">new</span> String(bytes,0<span style="color: #000000;">,len));
        }
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (FileNotFoundException e) {
        e.printStackTrace();
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
        e.printStackTrace();
    }
}

</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> BuffRead(){
    File file </span>= <span style="color: #0000ff;">new</span> File("d:\\test\\t.txt"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        InputStream in </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> FileInputStream(file);
        BufferedInputStream bis </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> BufferedInputStream(in);
        </span><span style="color: #0000ff;">byte</span>[] bytes = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[1024<span style="color: #000000;">];
        </span><span style="color: #0000ff;">int</span> len = -1<span style="color: #000000;">;
        </span><span style="color: #0000ff;">while</span> ((len = bis.read(bytes))!=-1<span style="color: #000000;">){
            System.out.println(</span><span style="color: #0000ff;">new</span> String(bytes,0<span style="color: #000000;">,len));
        }
        bis.close();
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (FileNotFoundException e) {
        e.printStackTrace();
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
        e.printStackTrace();
    }
}


</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> BuffWrite(){
    File file </span>= <span style="color: #0000ff;">new</span> File("d:\\test\\t.txt"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        OutputStream out </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> FileOutputStream(file);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">构造一个字节缓冲流</span>
        BufferedOutputStream  bos = <span style="color: #0000ff;">new</span><span style="color: #000000;"> BufferedOutputStream(out);
        String info </span>= "我是落魄书生"<span style="color: #000000;">;

        bos.write(info.getBytes());
        bos.close();

    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (FileNotFoundException e) {
        e.printStackTrace();
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
        e.printStackTrace();
    }

}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {

// BuffWrite(); BuffRead2(); } }

 

本文转载自博客园,原文链接:https://www.cnblogs.com/lpss-75074038/p/11974024.html

全部评论: 0

    我有话说: