zoukankan      html  css  js  c++  java
  • java 缓冲流 Buffer

    缓冲流 Buffer :设置缓冲区加快执行效率

    子类:

     (一)BufferedInputStream : 缓冲输入字节流 ,目的:提高读取文件的效率
       注意: BufferedInputStream 他是没有读写数据的功能
      内部实现 : 你面维护了一个8字节的byte数组。
      使用步骤:
            1.找到一个目标文件.
            2.建立通道 new FileInputStream(" ");
            3.创建一个缓冲字节输入流  new BufferedInputStream(FileInputStream);
            4.读取数据 read();
            5.关闭资源 close();
            

     (二)BufferedOutputStream :缓冲输出字节流    内部维护了一个 8k的字节数组
      作用: 提高文件的输出的效率,可以提供其他的方法。
      使用:
        1.找目标
        2.建立通道 FileOutputStream
        3.创建一个缓冲字节输出流
        4.写数据,不会写入到磁盘中。  如果数组中的数据已经满了,才会自动将数据写入到磁盘中。
        5.将数据写入到磁盘 : 调用flush(),或者关闭资源。
        6.关闭资源。


      (三)BuffredRead 缓冲输入字符流。
           有一个扩展的功能:readLine(); // 可以一次读一行文字。

     (四)BufferedWriter: 缓冲输出字符流
              内部提供一个8192长度的字符数组作为这样一个缓冲区。
          BufferedWriter作用 :提高写入的效率,拓展FileWriter的功能。

        newLine();  //换行写入数据

    简单的字节缓冲流案例

     1 import java.io.BufferedInputStream;
     2 import java.io.BufferedOutputStream;
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 
     8 public class buffered {
     9 
    10     /**
    11      * @param args
    12      * @throws IOException 
    13      */
    14     public static void main(String[] args) throws IOException {
    15         // TODO Auto-generated method stub
    16         //bufInTest();
    17         bufOutTest();
    18     }
    19     
    20     //(1)BufferedInputStream  的使用
    21     public static void bufInTest() throws IOException{
    22         //1.找到目标
    23         File file = new File("D:\a.txt");
    24         //2.创建通道
    25         FileInputStream fis = new FileInputStream(file);
    26 
    27         //**3.创建一个缓冲输入字节流******
    28         BufferedInputStream bfis = new BufferedInputStream(fis);
    29 
    30         //4.开始写入数据
    31          int content = 0; // 一次只会取一个字节
    32         while ((content= bfis.read())!= -1) { // 还是一个一个的读取 问什么可以提高效率呢?
    33             System.out.print((char)content);
    34         }
    35         //5.关闭通道
    36         bfis.close();
    37     }
    38     
    39     //(2)BufferedOutputStream   的使用
    40     public static void bufOutTest() throws IOException{
    41         
    42         //1.找目标
    43         File file = new File("D:\b.txt");
    44         //2.创建通道
    45         FileOutputStream fos = new FileOutputStream(file);
    46         
    47         //3.创建缓冲流
    48         BufferedOutputStream bfos = new BufferedOutputStream(fos);
    49         
    50         //4.创建写入文件的数据
    51         String string  = "good good study day day up";
    52         
    53         //5.写数据, 到这一步只是将数据保存到内存中字节数组中。
    54         bfos.write(string.getBytes()); 
    55         
    56         //6.再刷新 将数据写入到磁盘中
    57         //bfos.flush();
    58         
    59         //7.关闭资源
    60         bfos.close();//内部会实现flush();
    61     }
    62 }

    简单的字符缓冲流案例

     1 import java.io.BufferedReader;
     2 import java.io.BufferedWriter;
     3 import java.io.File;
     4 import java.io.FileReader;
     5 import java.io.FileWriter;
     6 import java.io.IOException;
     7 
     8 public class fileReader {
     9 
    10     public static void main(String[] args) throws IOException {
    11         testBufferedWriter();
    12         testBufferedRead();
    13     }
    14         //(1)缓冲输出字符流的使用
    15     public static void testBufferedWriter() throws IOException{
    16         
    17         //1.建立一个通道,指定一个路径
    18         FileWriter fiw = new FileWriter("D:\a.txt",true);
    19 
    20         //2.创建缓冲流
    21         BufferedWriter bfw = new BufferedWriter(fiw);
    22 
    23         //让数据换行显示
    24         bfw.newLine();  //换行写入数据
    25 
    26         //3.开始写入数据
    27         bfw.write("上课不要吃东西");
    28 
    29         //4.关闭资源
    30         bfw.close(); // 关闭资源之前会做一个刷新操作。调用flush()方法。    
    31     }
    32     
    33     //(2)缓冲输入字符流的使用
    34     public static void testBufferedRead() throws IOException{
    35         
    36         //1.创建通道并且指定路径
    37         FileReader fir = new FileReader("D:\b.txt");
    38 
    39         //2.创建缓冲流
    40         BufferedReader bfr = new BufferedReader(fir);
    41 
    42         //3.1、开始读取数据(一次读取一个字节)
    43         int content = 0;
    44         while ((content = bfr.read()) != -1) {
    45             
    46             System.out.print((char)content);
    47         }
    48         
    49         //3.2、readLine()扩展功能,读取一行数据    
    50         String string1 = bfr.readLine();
    51         System.out.println(string1);
    52         
    53         //一次读取一行数据(返回字符串),效率更高
    54         String string = null;
    55         while ((string = bfr.readLine()) != null) {
    56             System.out.println(string);
    57         }
    58         
    59         //4.关闭
    60         bfr.close();
    61     }
    62 }        
  • 相关阅读:
    ClickOnce發布經驗
    reporting Server組件不全引起的致命錯誤
    異步調用
    Usercontrol Hosted in IE
    MATLAB命令大全(转载)
    一种保护眼睛的好方法
    关于oracle自动编号
    An Algorithm Summary of Programming Collective Intelligence (1)
    An Algorithm Summary of Programming Collective Intelligence (3)
    An Algorithm Summary of Programming Collective Intelligence (4)
  • 原文地址:https://www.cnblogs.com/bigerf/p/6144224.html
Copyright © 2011-2022 走看看