BufferedInputStream Class

Package: java.io

The BufferedInputStream class reads characters from an input stream, using a buffer for increased efficiency. (A buffer is a temporary storage area that allows your program to read a large amount of data from disk at one time, and then hold it there until your program needs it.)

Note that you won’t typically work directly with the Buffered InputStream class. Instead, you’ll use it to connect to a DataInputStream, which has more advanced features for reading input data from binary files. For more information, see DataInputStream Class.

CrossRef.eps The BufferedInputStream class is one of many Java I/O classes that use streams. For more information, see Streams (Overview).

Constructor

Constructor

Description

BufferedInputStream (InputStream in)

Creates a buffered input stream from any object that extends the InputStream class. Typically, you pass this constructor a FileInputStream object.

Methods

Method

Description

int available()

Returns the number of bytes available in the input stream.

void close()

Closes the file.

int read()

Reads a single character from the input stream and returns it as an integer. The method returns –1 if the end of the file has been reached. It throws IOException.

int read(char[] buf, int offset, int max)

Reads multiple characters into an array. Offset provides an offset into the array if you don’t want to read the characters into the start of the array. Max specifies the maximum number of characters to read. Returns the number of characters read, or -1 if the end of the input has been reached. This method throws IOException if an I/O error occurs.

void skip(long num)

Skips ahead the specified number of characters.

Creating a BufferedInputStream object

A BufferedInputStream is usually created from a File InputStream, which is in turn created from a File. For example:

File f;

FileInputStream fstream;

BufferedInput in;

f = new File(“myfile.txt”);

fstream = new FileInputStream(f);

in = new BufferedInputStream(fstream);

CrossRef.eps For more information about the file class, see file Class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset