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);
// 指定文件带缓冲区的读取流且指定缓冲区大小为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用法简介的更多相关文章
- Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream
Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...
- J05-Java IO流总结五 《 BufferedInputStream和BufferedOutputStream 》
1. 概念简介 BufferedInputStream和BufferedOutputStream是带缓冲区的字节输入输出处理流.它们本身并不具有IO流的读取与写入功能,只是在别的流(节点流或其他处理流 ...
- 节点流和处理流(BufferedReader和BufferedWriter,BufferedInputStream和BufferedOutputStream,ObjectlnputStream和objectOutputStream)
一.基本介绍: 1.节点流可以从一个特定的数据源读写数据,如FileReader. FileWriter 如图:字节流是直接对数据源(文件,数组之类存放数据的地方)进行操作 2.处理流(也叫包装流)是 ...
- IOS NSInvocation用法简介
IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...
- JodaTime用法简介
JodaTime用法简介 Java的Date和Calendar用起来简直就是灾难,跟C#的DateTime差距太明显了,幸好有JodaTime 本文简单罗列JodaTime的用法 package co ...
- Apache自带压力测试工具ab用法简介
ab命令原理 ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL进行访问.它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx.lighthttp ...
- Postman用法简介
转自:http://blog.csdn.net/flowerspring/article/details/52774399 Postman用法简介 转载 2016年10月10日 09:04:10 10 ...
- MSSQL Sql加密函数 hashbytes 用法简介
转自:http://www.maomao365.com/?p=4732 一.mssql sql hashbytes 函数简介 hashbytes函数功能为:返回一个字符,通过 MD2.MD4.MD5. ...
- java assert的用法简介【转】
assert的基本用法 assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制,如C,C++和Eiffel等,但是支持的形式不尽相同,有的是通过语言本身.有的是通过库 ...
随机推荐
- 《Ext JS 4.2 实战》可以买了
今天编辑告诉我,在网上可以买到这书了,购买链接是http://www.amazon.cn/Ext-JS-4-2%E5%AE%9E%E6%88%98-%E9%BB%84%E7%81%AF%E6%A1%A ...
- 【翻译】使用Sencha Touch开发Google Glass应用程序
原文:Developing for Google Glass with Sencha Touch 作者:Ross Gerbasi Ross Gerbasi is a Senior Engineer a ...
- 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...
- Hbase 备份的方式
HBase 备份的方式有三种: 1.下线备份 (1)停止集群. (2)Distcp (3)restore 2.在线备份 -replication 3.在线北大 -CopyTable 4.在线备份-Ex ...
- LeetCode(51)- Count and Say
题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...
- Apache Kafka简介与安装(二)
Kafka在Windows环境上安装与运行 简介 Apache kafka 是一个分布式的基于push-subscribe的消息系统,它具备快速.可扩展.可持久化的特点.它现在是Apache旗下的一个 ...
- seo建站需要注意哪些
seo建站是搜索引擎优化的意思,通过seo建站技术,我们可以为站点带来可观的流量.那么,要怎样才能做好seo建站?seo建站优化过程中应该注意的事项有哪些?这些问题,是每一个站长都关心的.鉴于此,笔者 ...
- JS基础:this的指向以及call、apply的作用
this 的指向 在具体的实际应用中,this 的指向无法在函数定义时确定,而是在函数执行的时候才确定的,根据执行时的环境大致可以分为以下3种: 1.当函数作为普通函数调用时,this 指向全局对象 ...
- Java Selenium 定位元素 实现的一个注册功能
import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Alert; impor ...
- Hyper Text Transfer Protocol(超文本传输协议)
HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...