Java BufferedInputStream read()方法与示例

BufferedInputStream类read()方法 (BufferedInputStream Class read() method)

Syntax:

句法:

    public int read();
    public int read(byte[] b_arr, int offset, int length);

  • read() method is available in java.io package.

    read()方法在java.io包中可用。

  • read() method is responsible to read the next bytes of data from the BufferedInputStream.

    read()方法负责从BufferedInputStream读取下一个数据字节。

  • read(byte[] b_arr, int offset, int length) method is used to read byte input stream till its length and paste it into the given byte array, starting at a given offset and it calls read() continuously of the underlying stream till any one of the condition become true.

    read(byte [] b_arr,int offset,int length)方法用于读取字节输入流,直到其长度并将其粘贴到给定的字节数组中,从给定的偏移量开始,并连续调用底层流的read()直到任何一个条件变为真。

  • These methods may throw an exception at the time of reading data to the stream.

    这些方法在将数据读取到流时可能会引发异常。

    IOException: This exception may throw when getting any input/output error while reading from.

    IOException :读取时出现任何输入/输出错误时,可能引发此异常。

  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

    这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the first case, read()

    在第一种情况下, read()

    • It does not accept any parameter.
  • In the second case, read(byte[] b_arr, int offset, int length)

    在第二种情况下, read(byte [] b_arr,int offset,int length)

    • byte [] b_arr – represents a byte array to be filled.
    • byte [] b_arr –表示要填充的字节数组。
    • int offset – represents the offset (i.e. starting position to store).
    • int offset –表示偏移量(即存储的起始位置)。
    • int length – represents the at most number of bytes to be read.
    • int length –表示最多要读取的字节数。

Return value:

返回值:

In both of the cases, the return type of the method is int,

在这两种情况下,方法的返回类型均为int 。

  • In the first case, it returns the bytes read.

    在第一种情况下,它返回读取的字节。

  • In the second case, it returns the total number of bytes to be read.

    在第二种情况下,它返回要读取的字节总数。

Example:

例:

// Java program to demonstrate the example 
// of read() method of BufferedInputStream

import java.io.*;

public class ReadOfBIS {
    public static void main(String[] args) throws Exception {
        // To open text file by using 
        // FileInputStream
        FileInputStream fis = new FileInputStream("D:includehelp.txt");

        // Instantiates BufferedInputStream 
        BufferedInputStream buff_stm = new BufferedInputStream(fis);

        byte[] b_arr = new byte[2];

        // By using read(b_arr,offset,length) method is to
        // read bytes into an array from this BufferedInputStream 
        buff_stm.read(b_arr, 0, 2);

        for (byte val: b_arr) {
            System.out.println("buff_stm.read(b_arr,0,2): " + (char) val);
        }

        int val1 = 0;

        // By using read() method is to
        // read a byte from this BufferedInputStream
        while ((val1 = buff_stm.read()) != -1) {
            // Convert int to char
            char ch = (char) val1;

            // Display ch
            System.out.println("buff_stm.read(): " + ch);
        }
        // Close the stream 
        fis.close();
        buff_stm.close();
    }
}

Output

输出量

buff_stm.read(b_arr,0,2): J
buff_stm.read(b_arr,0,2): a
buff_stm.read(): v
buff_stm.read(): a


翻译自: https://www.includehelp.com/java/bufferedinputstream-read-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值