java bufferedinputstream关闭_BufferedInputStream使用详解

下面的例子演示如何使用BufferedInputStream类读取文本文件内容。

首先需要声明一个byte数组作为buffer,然后循环将文本内容循环读入到buffer中,并将buffer转换为字符串,打印到控制台。

/**

*

* @author outofmemory.cn

*/

public class Main {

/**

* 从文件中读取文本

*/

public void readFromFile(String filename) {

BufferedInputStream bufferedInput = null;

byte[] buffer = new byte[1024];

try {

//创建BufferedInputStream 对象

bufferedInput = new BufferedInputStream(new FileInputStream(filename));

int bytesRead = 0;

//从文件中按字节读取内容,到文件尾部时read方法将返回-1

while ((bytesRead = bufferedInput.read(buffer)) != -1) {

//将读取的字节转为字符串对象

String chunk = new String(buffer, 0, bytesRead);

System.out.print(chunk);

}

} catch (FileNotFoundException ex) {

ex.printStackTrace();

} catch (IOException ex) {

ex.printStackTrace();

} finally {

//关闭 BufferedInputStream

try {

if (bufferedInput != null)

bufferedInput.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

/**

* @param args 命令行参数

*/

public static void main(String[] args) {

new Main().readFromFile("myFile.txt");

}

}

使用方法

BufferedInputStream继承于FilterInputStream,提供缓冲输入流功能。缓冲输入流相对于普通输入流的优势是,它提供了一个缓冲数组,每次调用read方法的时候,它首先尝试从缓冲区里读取数据,若读取失败(缓冲区无可读数据),则选择从物理数据源(譬如文件)读取新数据(这里会尝试尽可能读取多的字节)放入到缓冲区中,最后再将缓冲区中的内容部分或全部返回给用户.由于从缓冲区里读取数据远比直接从物理数据源(譬如文件)读取速度快。

方法介绍

BufferedInputStream提供的API如下:

//构造方法

BufferedInputStream(InputStream in)

BufferedInputStream(InputStream in, int size)

//下一字节是否可读

synchronized int available()

//关闭

void close()

//标记, readlimit为mark后最多可读取的字节数

synchronized void mark(int readlimit)

//是否支持mark, true

boolean markSupported()

//读取一个字节

synchronized int read()

//读取多个字节到b

synchronized int read(byte[] b, int off, int len)

//重置会mark位置

synchronized void reset()

//跳过n个字节

synchronized long skip(long n)

使用示例

public void testBufferedInput() {

try {

/**

* 建立输入流 BufferedInputStream, 缓冲区大小为8

* buffer.txt内容为

* abcdefghij

*/

InputStream in = new BufferedInputStream(new FileInputStream(new File("buff.txt")), 8);

/*从字节流中读取5个字节*/

byte [] tmp = new byte[5];

in.read(tmp, 0, 5);

System.out.println("字节流的前5个字节为: " + new String(tmp));

/*标记测试*/

in.mark(6);

/*读取5个字节*/

in.read(tmp, 0, 5);

System.out.println("字节流中第6到10个字节为: " + new String(tmp));

/*reset*/

in.reset();

System.out.printf("reset后读取的第一个字节为: %c" , in.read());

} catch (Exception e) {

e.printStackTrace();

}

}

运行结果如下:

字节流的前5个字节为: abcde

字节流中第6到10个字节为: fghij

reset后读取的第一个字节为: f

使用BufferedInputStream和BufferedOuputStream读写图片

使用方式和FileInputStrem和FileOutputStream基本一致:

packageorg.example.io;

importjava.io.BufferedInputStream;

importjava.io.BufferedOutputStream;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileOutputStream;

publicclassTestBufferedString {

publicstaticvoidmain(String[] args) throwsException {

// 指定要读取文件的缓冲输入字节流

BufferedInputStream in = newBufferedInputStream(newFileInputStream("F:\\test.jpg"));

File file = newFile("E:\\test.jpg");

if(file != null) {

file.createNewFile();

}

// 指定要写入文件的缓冲输出字节流

BufferedOutputStream out = newBufferedOutputStream(newFileOutputStream(file));

byte[] bb = newbyte[1024];// 用来存储每次读取到的字节数组

intn;// 每次读取到的字节数组的长度

while((n = in.read(bb)) != -1) {

out.write(bb, 0, n);// 写入到输出流

}

out.close();// 关闭流

in.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值