BufferedInputStream与BufferedOutputStream用法简介
BufferedInputStream是带缓冲区的输入流,默认缓冲区大小是8M,能够减少访问磁盘的次数,提高文件读取性能;BufferedOutputStream是带缓冲区的输出流,能够提高文件的写入效率。BufferedInputStream与BufferedOutputStream分别是FilterInputStream类和FilterOutputStream类的子类,实现了装饰设计模式。
BufferedInputStream类的例子如下:
import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import java.io.BufferedInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date;public class BufferedInputStreamDemo01{// 声明常量public static final int SIZE=1024;public static void main(String[] args){//变量声明File f=null;InputStream input=null;BufferedInputStream bis=null;StringBuilder strBuild=null;SimpleDateFormat sdf=null;Date d=null;long start=0L;long end=0L; try{sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");strBuild=new StringBuilder();start=System.currentTimeMillis();d=new Date();if(d!=null){d.setTime(start);}System.out.println("程序开始执行时间:"+sdf.format(d));f=new File("d:"+File.separator+"demo.txt");input=new FileInputStream(f);// 指定文件带缓冲区的读取流且指定缓冲区大小为2KBbis=new BufferedInputStream(input,2*SIZE);int bisLength=bis.available();int readLength=0;byte[] byteArray=new byte[SIZE];int tmp=0;while((tmp=bis.read(byteArray))!=-1){ strBuild.append(new String(byteArray,0,tmp));System.out.println("每次读取字节数量:"+tmp);System.out.println("文件中剩余字节数:"+input.available());}System.out.println(String.format("文件的大小:%d,缓冲区读取流返回的大小:%d",f.length(),bisLength));System.out.println("文件的内容:"+strBuild.toString());System.out.println("字符串长度:"+strBuild.toString().length());char[] cTmp=strBuild.toString().toCharArray();System.out.println("字符串->字符数组长度:"+cTmp.length);end=System.currentTimeMillis();d=new Date();if(d!=null){ d.setTime(end);}System.out.println("程序执行的结束时间:"+sdf.format(d));System.out.println("<-------------******************---------------->");System.out.println("程序执行时间(ms):"+(end-start)+"毫秒");}catch(FileNotFoundException ex){ex.printStackTrace();}catch(IOException ex){ex.printStackTrace();}finally{try{if(input!=null){input.close();}if(bis!=null){bis.close(); }}catch(IOException ex){ex.printStackTrace();}}} }
BufferedOutputStream类的例子如下:
import java.io.File; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;public class BufferedOutputStreamDemo01{public static final int SIZE=1024;public static final String DRIVERNAME="oracle.jdbc.driver.OracleDriver";public static final String DBURL="jdbc:oracle:thin:@IP:1521:DB名称";public static final String USERNAME="用户名";public static final String PASSWORD="密码";static{try{// 加载驱动程序Class.forName(DRIVERNAME);}catch(ClassNotFoundException ex){ex.printStackTrace();}}public static void main(String[] args){// 变量声明File f=null;OutputStream output=null; BufferedOutputStream bos=null;Connection con=null;PreparedStatement pst=null;ResultSet rs=null;StringBuilder strBuild=null;try{String sql=" select vendor_no,vendor_name,address,phone,email,zipcode from VENDOR";con=new BufferedOutputStreamDemo01().getConnection();// 获得数据库操作类pst=new BufferedOutputStreamDemo01().getPst(con,sql);// 获得结果集rs=pst.executeQuery();f=new File("F:"+File.separator+"tmp.txt");output=new FileOutputStream(f,false);bos=new BufferedOutputStream(output,SIZE*4);while(rs.next()){strBuild=new StringBuilder();// 店号strBuild.append(rs.getString("vendor_no"));strBuild.append(",");// 店名strBuild.append(rs.getString("vendor_name"));strBuild.append(",");// 地址strBuild.append(rs.getString("address"));strBuild.append(",");// 电话strBuild.append(rs.getString("phone"));strBuild.append(",");// 邮件strBuild.append(rs.getString("email"));strBuild.append(",");// 邮政编码strBuild.append(rs.getString("zipcode"));strBuild.append("\n");bos.write(strBuild.toString().getBytes("utf-8"));}}catch(IOException ex1){ex1.printStackTrace();}catch(SQLException ex){ex.printStackTrace();}finally{try{// 关闭流if(output!=null){output.close(); }if(bos!=null){bos.close();} //关闭数据库连接if(rs!=null){rs.close();}if(pst!=null){pst.close();}if(con!=null){con.close();}}catch(IOException ex){ex.printStackTrace();}catch(SQLException ex){ex.printStackTrace();}}}/****获得数据库连接****/public static Connection getConnection(){Connection con=null;try{// 获得数据库连接con=DriverManager.getConnection(DBURL,USERNAME,PASSWORD);}catch(SQLException ex){ex.printStackTrace();} return con;}/****获得数据库操作类**/public static PreparedStatement getPst(Connection con,String sql){PreparedStatement pst=null;try{pst=con.prepareStatement(sql);}catch(SQLException ex){ex.printStackTrace();} return pst;} }
以上带缓冲区的输入输出流实例希望对初学者有所帮助,望批评指正!
BufferedInputStream与BufferedOutputStream用法简介相关推荐
- Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream
Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...
- MSSQL Sql加密函数 hashbytes 用法简介
原文:MSSQL Sql加密函数 hashbytes 用法简介 转自:http://www.maomao365.com/?p=4732 一.mssql sql hashbytes 函数简介 hashb ...
- Postman用法简介-Http请求模拟工具
Postman用法简介-Http请求模拟工具 在我们平时开发中,特别是需要与接口打交道时,无论是写接口还是用接口,拿到接口后肯定都得提前测试一下,这样的话就非常需要有一个比较给力的Http请求模拟工具 ...
- InputStreamReader 和 OutputStreamWriter类用法简介,及演示。
InputStreamReader 和 OutputStreamWriter类用法简介. 一.InputStreamReader类 InputStreamReader 将字节流转换为字符流.是字节流通 ...
- php中__FILE__常量用法简介
php中__FILE__常量用法简介 http://blog.csdn.net/xbei07/article/details/5616020#comments出处 1.php中的__FILE__常量返 ...
- java test 用法,pytest基本用法简介
1.安装pytest,打开dos窗口输入: pip install pytest 2.通过pycharm工具下载 3.创建pytest测试用例步骤 # 定义测试类 class testdivide: ...
- SAP Gateway与OData用法简介
本文将分别从基础原理和实际应用两个角度介绍SAP Gateway与OData相关的知识点. 1 相关的事务代码(t-code) SEGW - SAP Gateway Service Builder / ...
- Matlab中 intlinprog函数用法简介
Matlab中 intlinprog函数用法简介 本来想要自己亲手写一遍的,发现了一优质博文基本上跟我做过的例题大差不差,所以就直接放上链接. 参考链接 https://www.cnblogs.com ...
- intersect的用法简介
intersect的用法简介 在Oracle中,"A minus B"用于将结果集B中含有的记录从结果集A中移除,即用于获取存在于结果集A中而不存在于结果集B中的记录:" ...
最新文章
- python语音翻译软件下载_python软件翻译
- linux7开放3306端口,CentOS 7 开放3306端口访问
- java设计模式---职责链模式
- sql2005备份还原详解
- python免费开源工具推荐_年薪200万的程序员,推荐这10大Python免费开源工具!
- u盘锁电脑_程序员用U盘偷公司源码被抓;Rust 语言发布 5 周年
- 小结-python连接redis的基本配置-解决远程连接问题
- StringUtils,FileUtils,PropertiesUtils,DataBaseUtils
- 怎样远程连接Access数据库
- 高等代数第3版下 [丘维声 著] 2015年版_2020年成人高考 专升本 高等数学复习攻略...
- Linux系统上QQ闪退的问题
- hdu2553解题报告
- 关于谷歌不兼容showModalDialog的解决方案
- ASPUPload3.0注册机
- 基于 Sphinx 以纯文本编写富媒体项目文档的方法介绍
- hr面试性格测试30题_HR性格测试题.doc
- 周期性的方波 matlab,[转载]matlab产生方波脉冲和周期性方波信号
- hadoop无法自动生成tmp文件
- mysql内存回收_mysql占用内存过多
- MATLAB灰度图像缩小实验