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. 基于ROS_Arduino室内移动机器人SLAM实验测试

    纯手工搭建的机器人,因此外观并不美. 基于ROS(indigo)以及Arduino等搭建软硬件平台,包括语音.视觉.激光.码盘等传感器设备. 整体如下图所示: 底盘特写: 语音输入: Arduino模 ...

  2. 【Android 系统开发】下载 编译 Android源代码 和 Android kernel源代码

    下载Android源码简要流程 : a. 获取repo文件: curl http://commondatastorage.googleapis.com/git-repo-downloads/repo ...

  3. 【一天一道leetcode】 #2 Add Two Numbers

    一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...

  4. 数据挖掘进阶之关联规则挖掘FP-Growth算法

    数据挖掘进阶之关联规则挖掘FP-Growth算法 绪 近期在写论文方面涉及到了数据挖掘,需要通过数据挖掘方法实现软件与用户间交互模式的获取.分析与分类研究.主要涉及到关联规则与序列模式挖掘两块.关联规 ...

  5. Spring--FileSystemXmlApplicationContext

    //从文件系统或者统一定位资源中获得上下文的定义 public class FileSystemXmlApplicationContext extends AbstractXmlApplication ...

  6. Linux变量键盘读取、数组与声明: read, array, declare

    [root@www ~]# read [-pt] variable 选项与参数: -p :后面可以接提示字符! -t :后面可以接等待的『秒数!』这个比较有趣-不会一直等待使用者啦! 范例一:让用户由 ...

  7. 苹果新的编程语言 Swift 语言进阶(十一)--实例的初始化与类的析构

    一 .实例的初始化          实例的初始化是准备一个类.结构或枚举的实例以便使用的过程.初始化包括设置一个实例的每一个存储属性为一个初始值,以及执行任何其它新的实例能够使用之前需要的设置或初始 ...

  8. HBase学习资源

    教程 <HBase.Administration.Cookbook>  中文版<HBase管理指南> <HBase in action> <HBase权威指南 ...

  9. PHP-MVC和Smarty初探笔记

    在慕课网上学习了PHP的MVC的基础知识,记录一下笔记: 等待更新~

  10. 抛开rails使用ActiveRecord连接数据库

    今天是大年三十,明天就正式进入羊年鸟,给所有程序猿(媛)同人拜个年吧!祝大家身体健康,事业有成,财源广进哦! 话归正题,以前都是在rails中使用数据库,或者在rails的console中使用:我们如 ...