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等,但是支持的形式不尽相同,有的是通过语言本身.有的是通过库 ...
随机推荐
- 树莓派linux驱动学习之LED控制
前面我们编写了hello world的程序,接下来继续研究GPIO功能,通过GPIO来控制LED的亮灭,这在单片机中应该算是十分简单的一个程序了,但是在Linux系统中控制GPIO没有那么简单,难点就 ...
- 我也来写DBUtils
关于重复造轮子 作为一个程序员,我们不止一次听到师长前辈们说:不要重复造轮子,已经有现成的了,直接用就是了. 对于这个观点,我觉得得仔细分析分析. 如果我们正在做一个真实的项目,经理天天追在我们屁股后 ...
- lamp 环境配置
LAMP是一个缩写Linux+Apache+MySql+PHP,它指一组通常一起使用来运行动态网站或者服务器的自由软件: * Linux,操作系统:* Apache,网页服务器:* MySQL,数据库 ...
- RHEL自动安装zookeeper的shell脚本
RHEL自动安装zookeeper的shell脚本 A:本脚本运行的机器,Linux RHEL6 B,C,D,...:待安装zookeeper cluster的机器, Linux RHEL6 首先在脚 ...
- 虚拟机安装Ubuntu14.04打开FireFox提示Server not found
虚拟机安装Ubuntu14.04打开FireFox提示Server not found 我采用VMware安装ubuntu14.04的,VMware的网络是配置采用NAT模式(用于共享主机的IP地址) ...
- RecyclerView添加Header和Footer
使用过RecyclerView的同学就知道它并没有添加header和footer的方法,而ListView和GirdView都有,但是开发过程中难免有需求需要添加一个自定义的header或者foote ...
- OBJ-C中dispatch_once的用法
见如下代码: +(GameState*)sharedGameState{ static GameState *sharedInstance; static dispatch_once_t onceTo ...
- The content of elements must consist of well-formed character data or markup
java 中使用dom4j解析含有特殊字符的xml文件出现了如题的错误 这个时候需要在特殊字符外面加上 <![CDATA[ /6169220648+20671/1>7+-47390045& ...
- android studio设置代理更新
我们都知道Android Studio是基于IDEA开发的,而我们写的每一个程序又都是有Gradle构建的,Gradle的优点可以说是很多,被很多程序员夸得没边,但是它有一个特点还是值得我们注意的.我 ...
- Oracle经常用到的一些函数
1.数字函数 NVL( string1, replace_with) :如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值,如果两个参数都为NULL ...