• BufferedInputStream与BufferedOutputStream用法简介


    BufferedInputStream是带缓冲区的输入流,默认缓冲区大小是8M,能够减少访问磁盘的次数,提高文件读取性能;BufferedOutputStream是带缓冲区的输出流,能够提高文件的写入效率。BufferedInputStream与BufferedOutputStream分别是FilterInputStream类和FilterOutputStream类的子类,实现了装饰设计模式。
    BufferedInputStream类的例子如下:

     1 import java.io.File;
     2 import java.io.InputStream;
     3 import java.io.FileInputStream;
     4 import java.io.BufferedInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.IOException;
     7 import java.text.SimpleDateFormat;
     8 import java.util.Date;
     9 
    10 public class BufferedInputStreamDemo01{
    11 
    12     // 声明常量
    13         public static final int SIZE=1024;
    14 
    15     public static void main(String[] args){
    16        //变量声明
    17            File f=null;
    18        InputStream input=null;
    19        BufferedInputStream bis=null;
    20            StringBuilder strBuild=null;
    21        SimpleDateFormat sdf=null;
    22        Date d=null;
    23            long start=0L;
    24        long end=0L; 
    25 
    26           try{
    27           sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    28           
    29           strBuild=new StringBuilder();
    30                   start=System.currentTimeMillis();
    31           d=new Date();
    32           if(d!=null){
    33                   d.setTime(start);
    34          }
    35           System.out.println("程序开始执行时间:"+sdf.format(d));
    36 
    37       f=new File("d:"+File.separator+"demo.txt");
    38           input=new FileInputStream(f);
    39           // 指定文件带缓冲区的读取流且指定缓冲区大小为2KB
    40       bis=new BufferedInputStream(input,2*SIZE);
    41           int bisLength=bis.available();
    42           int readLength=0;
    43           byte[] byteArray=new byte[SIZE];
    44           int tmp=0;
    45           while((tmp=bis.read(byteArray))!=-1){ 
    46                 strBuild.append(new String(byteArray,0,tmp));
    47         System.out.println("每次读取字节数量:"+tmp);
    48         System.out.println("文件中剩余字节数:"+input.available());
    49      }
    50           
    51           System.out.println(String.format("文件的大小:%d,缓冲区读取流返回的大小:%d",f.length(),bisLength));
    52       System.out.println("文件的内容:"+strBuild.toString());
    53       System.out.println("字符串长度:"+strBuild.toString().length());
    54       char[] cTmp=strBuild.toString().toCharArray();
    55       System.out.println("字符串->字符数组长度:"+cTmp.length);
    56 
    57       end=System.currentTimeMillis();
    58       d=new Date();
    59       if(d!=null){ 
    60          d.setTime(end);
    61       }
    62       System.out.println("程序执行的结束时间:"+sdf.format(d));
    63       System.out.println("<-------------******************---------------->");
    64       System.out.println("程序执行时间(ms):"+(end-start)+"毫秒");
    65 
    66        }catch(FileNotFoundException ex){
    67           ex.printStackTrace();
    68        }catch(IOException ex){
    69           ex.printStackTrace();
    70        }finally{
    71          try{
    72                if(input!=null){
    73          input.close();
    74         }
    75            if(bis!=null){
    76          bis.close(); 
    77            }
    78          }catch(IOException ex){
    79            ex.printStackTrace();
    80          }
    81       }
    82    }
    83 }

    BufferedOutputStream类的例子如下:

      1 import java.io.File;
      2 import java.io.OutputStream;
      3 import java.io.FileOutputStream;
      4 import java.io.BufferedOutputStream;
      5 import java.io.FileNotFoundException;
      6 import java.io.IOException;
      7 import java.sql.Connection;
      8 import java.sql.DriverManager;
      9 import java.sql.PreparedStatement;
     10 import java.sql.ResultSet;
     11 import java.sql.SQLException;
     12 
     13 public class BufferedOutputStreamDemo01{
     14         public static final int SIZE=1024;
     15         public static final String DRIVERNAME="oracle.jdbc.driver.OracleDriver";
     16         public static final String DBURL="jdbc:oracle:thin:@IP:1521:DB名称";
     17     public static final String USERNAME="用户名";
     18     public static final String PASSWORD="密码";
     19     
     20     static{
     21        try{
     22             // 加载驱动程序
     23             Class.forName(DRIVERNAME);
     24           }catch(ClassNotFoundException ex){
     25             ex.printStackTrace();
     26         }
     27     }
     28 
     29     public static void main(String[] args){
     30        // 变量声明
     31        File f=null;
     32        OutputStream output=null; 
     33        BufferedOutputStream bos=null;
     34            Connection con=null;
     35        PreparedStatement pst=null;
     36            ResultSet rs=null;
     37        StringBuilder strBuild=null;
     38  
     39            try{
     40           String sql=" select vendor_no,vendor_name,address,phone,email,zipcode from VENDOR";
     41            
     42           con=new BufferedOutputStreamDemo01().getConnection();
     43                   // 获得数据库操作类
     44                   pst=new BufferedOutputStreamDemo01().getPst(con,sql);
     45                   // 获得结果集
     46           rs=pst.executeQuery();
     47 
     48           f=new File("F:"+File.separator+"tmp.txt");
     49           output=new FileOutputStream(f,false);
     50           bos=new BufferedOutputStream(output,SIZE*4);
     51           
     52           while(rs.next()){
     53                         strBuild=new StringBuilder();
     54             
     55             // 店号
     56             strBuild.append(rs.getString("vendor_no"));
     57             strBuild.append(",");
     58 
     59                         // 店名
     60             strBuild.append(rs.getString("vendor_name"));
     61             strBuild.append(",");
     62             
     63             // 地址
     64             strBuild.append(rs.getString("address"));
     65             strBuild.append(",");
     66             
     67             // 电话
     68             strBuild.append(rs.getString("phone"));
     69             strBuild.append(",");
     70 
     71                         // 邮件
     72             strBuild.append(rs.getString("email"));
     73             strBuild.append(",");
     74 
     75                         // 邮政编码
     76             strBuild.append(rs.getString("zipcode"));
     77             strBuild.append("
    ");
     78             
     79                         bos.write(strBuild.toString().getBytes("utf-8"));
     80           }
     81 
     82            }catch(IOException ex1){
     83                ex1.printStackTrace();
     84        }catch(SQLException ex){
     85            ex.printStackTrace();
     86        }finally{
     87                try{
     88              // 关闭流
     89              if(output!=null){
     90                output.close(); 
     91              }
     92              if(bos!=null){
     93                             bos.close();
     94              } 
     95              //关闭数据库连接
     96              if(rs!=null){
     97                             rs.close();
     98              }
     99                          if(pst!=null){
    100                             pst.close();
    101              }
    102                          if(con!=null){
    103                con.close();
    104              }
    105                  }catch(IOException ex){
    106                 ex.printStackTrace();
    107                  }catch(SQLException ex){
    108                     ex.printStackTrace();
    109              }
    110          }
    111     }
    112     
    113     /**
    114         **获得数据库连接
    115     **
    116     **/
    117     public static Connection getConnection(){
    118        Connection con=null;
    119        try{
    120            // 获得数据库连接
    121            con=DriverManager.getConnection(DBURL,USERNAME,PASSWORD);
    122        }catch(SQLException ex){
    123            ex.printStackTrace();
    124        } 
    125 
    126        return con;
    127     }
    128     
    129     /**
    130         **获得数据库操作类
    131     **/
    132     public static PreparedStatement getPst(Connection con,String sql){
    133             PreparedStatement pst=null;
    134         try{
    135           pst=con.prepareStatement(sql);
    136            }catch(SQLException ex){
    137           ex.printStackTrace();
    138            } 
    139 
    140         return pst;
    141     }
    142 }
  • 相关阅读:
    Leetcode刷题记录--39. 组合总和
    Leetcode刷题记录--31. 下一个排列
    Leetcode刷题记录--22. 括号生成(回溯)
    Leetcode刷题记录--17. 电话号码的字母组合(回溯)
    分布式学习之--6.824MITLab1记录
    总结javascript处理异步的方法
    引用、浅拷贝及深拷贝 到 Map、Set(含对象assign、freeze方法、WeakMap、WeakSet及数组map、reduce等等方法)
    Vue之富文本tinymce爬坑录
    iOS 13 正式发布,来看看有哪些 API 变动
    Vuex,从入门到...
  • 原文地址:https://www.cnblogs.com/shamo89/p/7640801.html
Copyright © 2020-2023  润新知