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);
// 指定文件带缓冲区的读取流且指定缓冲区大小为2KB
bis=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用法简介的更多相关文章

  1. Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream

    Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...

  2. J05-Java IO流总结五 《 BufferedInputStream和BufferedOutputStream 》

    1. 概念简介 BufferedInputStream和BufferedOutputStream是带缓冲区的字节输入输出处理流.它们本身并不具有IO流的读取与写入功能,只是在别的流(节点流或其他处理流 ...

  3. 节点流和处理流(BufferedReader和BufferedWriter,BufferedInputStream和BufferedOutputStream,ObjectlnputStream和objectOutputStream)

    一.基本介绍: 1.节点流可以从一个特定的数据源读写数据,如FileReader. FileWriter 如图:字节流是直接对数据源(文件,数组之类存放数据的地方)进行操作 2.处理流(也叫包装流)是 ...

  4. IOS NSInvocation用法简介

    IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...

  5. JodaTime用法简介

    JodaTime用法简介 Java的Date和Calendar用起来简直就是灾难,跟C#的DateTime差距太明显了,幸好有JodaTime 本文简单罗列JodaTime的用法 package co ...

  6. Apache自带压力测试工具ab用法简介

    ab命令原理 ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL进行访问.它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx.lighthttp ...

  7. Postman用法简介

    转自:http://blog.csdn.net/flowerspring/article/details/52774399 Postman用法简介 转载 2016年10月10日 09:04:10 10 ...

  8. MSSQL Sql加密函数 hashbytes 用法简介

    转自:http://www.maomao365.com/?p=4732 一.mssql sql hashbytes 函数简介 hashbytes函数功能为:返回一个字符,通过 MD2.MD4.MD5. ...

  9. java assert的用法简介【转】

    assert的基本用法 assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制,如C,C++和Eiffel等,但是支持的形式不尽相同,有的是通过语言本身.有的是通过库 ...

随机推荐

  1. 通过CSS显示垂直文本

    原文链接: CSS Vertical Text 原文日期: 2014年03月18日 翻译日期: 2014年3月22日 翻译人员: 铁锚 示例地址: http://davidwalsh.name/dem ...

  2. MVC学习笔记(一)

    首先感谢慕课网这个平台提供给我的学习机会,感谢PengCheng老师的"MVC架构模式分析与设计课程". 1.数组的声明: $controllerAllow = array('te ...

  3. PDA开发数据由DB下载至PDA本地

    public string DownFile = "\\下载.txt";//下载路径 public string LoadFile = "\\上传.txt";/ ...

  4. LIRe 源代码分析 4:建立索引(DocumentBuilder)[以颜色布局为例]

    ===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...

  5. 为macbook双系统的windows装驱动

    网上有很多装双系统教程,这里就不再累赘,但是自己发现装完后驱动怎么装并没有交代清楚. 研究后发现,在作为驱动盘的U盘里,BootCamp文件夹下有个setup.exe 运行此程序便进行驱动的安装.

  6. vim多行增加缩进

    http://blog.163.com/clevertanglei900@126/blog/static/11135225920116891750734/ 在Normal Mode下,命令>&g ...

  7. Remove Google Play Games libraries on iOS (Unity3D开发之二十一)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/48313653 ...

  8. Oracle Global Finanicals Technical Reference(一)

    Skip Headers Oracle Global Finanicals Oracle Global Financials Technical Reference Manual Release 11 ...

  9. hadoop小知识札记

    hadoop实现全局变量: 只读的可以,可修改的不行,只读的可以通过configuration 或者分布式缓存实现.   hadoop做图像处理时,每个map读入一个图片,每个map读入一张图片,然后 ...

  10. SoC嵌入式软件架构设计

    内存是SoC(System on Chip,片上系统)集成设计的重要模块,是SoC中成本比重较大的部分.内存管理的软硬件设计是SoC软件架构设计的重要一环,架构设计师必须要在成本和效率中取得平衡,做到 ...