BufferedInputStream类方法,使用BufferedInputStream类读取文本文件内容

开发 后端
本文主要学习BufferedInputStream类方法,使用BufferedInputStream类读取文本文件内容、BufferedOutputStream类向文件中写入内容和它的常用方法,接下来小编带大家一起来学习!

[[438687]]

大家好,我是Java进阶者。

前言

本文主要学习BufferedInputStream类方法,使用BufferedInputStream类读取文本文件内容、BufferedOutputStream类向文件中写入内容和它的常用方法,接下来小编带大家一起来学习!

一、BufferedInputStream类方法

1.BufferedInputStream是缓冲输入流,可以减少访问磁盘的次数,提高文件的读取性能,它是FilterInputStream类的子类。

2.BufferedInputStream类方法有:

(1)int available()方法:用于返回输入流中可用的未读字节数,而不会由于下一次为此InputStream的方法的调用而阻塞。

(2)void close()方法:关闭此输入流并释放与该流关联的所有系统资源。

(3)void mark(int readlimit)方法:输入流的当前位置做个标记,readlimit参数是输入流在标记位置失效前允许读取的字节数。

(4)boolean markSupported()方法:测试输入流是否支持mark和reset方法。

(5)int read()方法:读取一个字节。

(6)int read(byte[] b, int off, int len)方法:读取多个字节到字节数组b中,参数off是数组偏移量,参数len是读取数据的长度。

(7)void reset()方法:重置流的当前位置到前面标记的位置。

(8)long skip(long n)方法:略过流中的数据。若数据不够时,跳过仅有的字节,返回跳过的字节数。

二、BufferedInputStream类read(byte[] b, int off, int len)方法

1.public int read(byte[] b, int off, int len)方法:读取多个字节到字节数组b中,参数off是数组偏移量,参数len是读取数据的长度。

2.read(byte[] b, int off, int len)方法例子的实现:

(1)在text文件夹下创建一个test.txt文件并写入"helloworld,java!"内容。

(2)建立输入流BufferedInputStream, 缓冲区大小为8,读取字节流的前5个字节的代码的实现。

  1. public class P09 { 
  2. public static void main(String[] args) throws Exception { 
  3.         // TODO Auto-generated method stub 
  4.         //创建一个带有缓冲区的输入流 
  5.         BufferedInputStream in = new BufferedInputStream(new FileInputStream("text/test"), 8); 
  6.         //从字节流中读取5个字节 
  7.         byte temp[]=new byte[5]; 
  8.         //read(byte[] b, int offint len)方法 
  9.         in.read(temp,0,5); 
  10.         System.out.println("字节流的前5个字节是:"+new String(temp)); 

运行的结果如下图所示:

三、BufferedInputStream类mark()和reset()方法

1.void mark(int readlimit)方法:输入流的当前位置做个标记,readlimit参数是输入流在标记位置失效前允许读取的字节数。

2.void reset()方法:重置流的当前位置到前面标记的位置。

3.例子的实现:

  1. import java.io.*; 
  2.  
  3. public class P09 { 
  4. public static void main(String[] args) throws Exception { 
  5.         // TODO Auto-generated method stub 
  6.         //创建一个带有缓冲区的输入流 
  7.         BufferedInputStream in = new BufferedInputStream(new FileInputStream("text/test"), 8); 
  8.         //从字节流中读取5个字节 
  9.         byte temp[]=new byte[5]; 
  10.         //read(byte[] b, int offint len)方法 
  11.         in.read(temp,0,5); 
  12.         System.out.println("字节流的前5个字节是:"+new String(temp)); 
  13.         //标记测试 
  14.         in.mark(6); 
  15.         in.read(temp,0,5); 
  16.         System.out.println("字节流的第6到10个字节是:"+new String(temp)); 
  17.         //reset()方法 
  18.         in.reset(); 
  19.         System.out.printf("reset后读取的第一个字节为:%c"in.read()); 
  20.   } 

运行的结果如下图所示:

四、BufferedOutputStream类

1.BufferedOutputStream类是字节缓冲输出流,它是FilterOutputStream类的子类。

2.BufferedOutputStream类常用的方法有以下所示:

(1)void write(int b)方法:一次写一个字节。

(2)void write(byte[] b,int off,int len)方法:从指定数组b中的从偏移量off开始len个字节写入文件输出流中。off参数表示数组偏移量,len表示要写入的字节数。

(3)void flush()方法:刷新此缓冲的输出流。这迫使所有缓冲的输出字节被写出到底层输出流中。

(4)void close()方法:关闭此输入流并释放与该流关联的所有系统资源。

3.BufferedOutputStream方法的实现例子:

  1. import java.io.*; 
  2. public class P10 { 
  3. public static void main(String[] args) throws Exception { 
  4.         // TODO Auto-generated method stub 
  5.         //创建一个带缓冲流的输出流 
  6.         BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("text/test10")); 
  7.         //在文本文件中写入小写a字母 
  8.         bos.write(97); 
  9.         //在文本文件中写入"Java进阶学习交流" 
  10.         bos.write("\nJava进阶学习交流\n".getBytes()); 
  11.         //创建一个字节数组 
  12.         byte[] bytes = {97,98,99,100,101};  
  13.         //从偏移量2位置开始就是c,获取写入2个字节数 
  14.         bos.write(bytes,2,2); 
  15.         //刷新缓冲流 
  16.         bos.flush(); 
  17.         //关闭流 
  18.         bos.close();   
  19.   } 

运行的结果如下所示:

五、总结

本文主要介绍了BufferedInputStream类方法、BufferedOutputStream类。介绍了BufferedInputStream的read(byte[] b, int off, int len)方法、mark()和reset()方法通过例子理解这些方法用法,使用BufferedInputStream来读取文本的内容。BufferedOutputStream类是字节缓冲输出流,它是FilterOutputStream类的子类。BufferedOutputStream来写入文本的内容。希望大家通过本文的学习,对你有所帮助!

本文转载自微信公众号「Java进阶学习交流」,可以通过以下二维码关注。转载本文请联系Java进阶学习交流公众号。

 

责任编辑:武晓燕 来源: Java进阶学习交流
相关推荐

2021-11-29 09:46:11

FileReaderJava开发

2021-12-09 09:30:38

字节流文件缓冲区

2009-09-02 19:08:03

C#实现读取文本文件

2015-06-17 14:28:15

Java查询处理方法

2022-11-25 09:16:43

Linux命令

2009-08-12 17:59:48

C#读取文本文

2010-04-30 17:38:31

Unix文本

2022-09-29 10:01:05

Go编程语言文本文件

2009-09-02 19:13:08

C#处理文本文件

2009-08-06 18:33:45

C#处理文本文件

2010-01-08 16:10:05

VB.NET读写文本文

2010-01-15 10:05:35

VB.NET文件对象

2009-08-26 11:53:56

C#打印文本文件

2009-09-04 15:56:35

写入文本文件

2014-03-11 10:11:33

Linux命令more命令文本文件

2021-11-24 08:41:52

BufferedWriJavaBufferedRea

2019-02-13 09:20:13

Linux命令行more

2023-05-17 18:45:56

Linux文件

2009-08-19 17:44:15

C#操作文本文件

2010-02-01 14:26:50

C++读写文本文件
点赞
收藏

51CTO技术栈公众号