0% found this document useful (0 votes)
36 views2 pages

File Class

Buffered input streams read more data than initially needed into an internal buffer, and buffered output streams store data in an internal buffer until full or flushed. Wrapping unbuffered streams in buffered stream classes provides performance gains by reducing disk/network access when reading/writing several hundred bytes at once.

Uploaded by

RASHMI DABRE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

File Class

Buffered input streams read more data than initially needed into an internal buffer, and buffered output streams store data in an internal buffer until full or flushed. Wrapping unbuffered streams in buffered stream classes provides performance gains by reducing disk/network access when reading/writing several hundred bytes at once.

Uploaded by

RASHMI DABRE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

The File class is Java’s representation of a file or directory path name.

Because file and
directory names have different formats on different platforms, a simple string is not adequate
to name them. The File class contains several methods for working with the path name,
deleting and renaming files, creating new directories, listing the contents of a directory, and
determining several common attributes of files and directories.

Buffered Streams
Most of the examples we've seen so far use unbuffered I/O. This means each read or write request is
handled directly by the underlying OS. This can make a program much less efficient, since each such
request often triggers disk access, network activity, or some other operation that is relatively expensive.

To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input
streams read data from a memory area known as a buffer; the native input API is called only when the
buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called
only when the buffer is full.

A program can convert an unbuffered stream into a buffered stream using the wrapping idiom we've used
several times now, where the unbuffered stream object is passed to the constructor for a buffered stream
class. 

Buffered Streams
Buffered input streams read more data than they initially need into a
buffer (an internal array of bytes). When the
stream’s read() methods are invoked, the data is removed from the
buffer rather than the underlying stream. When the buffer runs out
of data, the buffered stream refills its buffer from the underlying
stream. Likewise, buffered output streams store data in an internal
byte array until the buffer is full or the stream is flushed; then the
data is written out to the underlying output stream in one swoop. In
situations where it’s almost as fast to read or write several hundred
bytes from the underlying stream as it is to read or write a single
byte, a buffered stream can provide a significant performance gain.

There are two BufferedInputStream constructors and


two BufferedOutputStream constructors:
public BufferedInputStream(InputStream in)

public BufferedInputStream(InputStream in, int size)

public BufferedOutputStream(OutputStream out)

public BufferedOutputStream(OutputStream out, int size)

The first argument is the underlying stream from which data will be
read or to which data will be written. The size argument is the
number of bytes in the buffer. If a size isn’t specified, a 2048-byte
buffer is used. The best size for the buffer depends on the platform
and is generally related to the block size of the disk (at least for file
streams). Less than 512 bytes is probably too small and more than
4096 bytes is probably too large. Ideally, you want an integral ...

You might also like