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;
}
}
04-30 19:42