原文网址:BufferedInputStream使用详解 – 有梦就能实现 – 博客园 (cnblogs.com)

下面的例子演示如何使用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基本一致:

 

  1. package org.example.io;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8.   
  9. public class TestBufferedString {  
  10.   
  11.     public static void main(String[] args) throws Exception {  
  12.         // 指定要读取文件的缓冲输入字节流  
  13.         BufferedInputStream in = new BufferedInputStream(new FileInputStream(“F:\\test.jpg”));  
  14.         File file = new File(“E:\\test.jpg”);  
  15.         if (file != null) {  
  16.             file.createNewFile();  
  17.         }  
  18.         // 指定要写入文件的缓冲输出字节流  
  19.         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));  
  20.         byte[] bb = new byte[1024];// 用来存储每次读取到的字节数组  
  21.         int n;// 每次读取到的字节数组的长度  
  22.         while ((n = in.read(bb)) != –1) {  
  23.             out.write(bb, 0, n);// 写入到输出流  
  24.         }  
  25.         out.close();// 关闭流  
  26.         in.close();  
  27.     }  
  28.   
  29. }  

原文地址:http://www.cnblogs.com/wi100sh/p/16876363.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性