Buffer:
This is the area of physical memory used to temporarily store data as it moves from one place to another. In most cases, this physical memory will be RAM ( random access memory).
But from this context of the question, the buffer is used when reading / writing data. It does not need to be used when moving data from one place to another.
An example for a buffer: if your system has 4 GB of RAM, the system can allocate 4 KB of memory (RAM) for the buffer. KB - kilobytes (s), GB - gigabytes (s)
I / O stream (or) Stream:
An input / output stream is an input source or output destination. A stream can represent many different types of sources and destinations, including disk files, devices, other programs, and memory arrays.
I / O means I / O.
Thus, an Input Stream can be an input source, such as a file on disk, a network connection, etc.
And, Output Stream can be an output destination, such as a file on disk, a network connection, etc.
According to the official JAVA documentation , there are three types of streams.
- Byte streams (read or write bytes)
- Character streams (reading or writing characters)
- Buffered streams (read or write to the buffer for efficiency)
Byte streams:
They perform input and output of 8-bit bytes. All byte stream classes are derived from InputStream and OutputStream .
Bytes The input classes of the stream receive the input as raw bytes . The output byte classes give the output as raw bytes .
InputStream - Direct Known Subclasses
AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream.
OutputStream - Direct Known Subclasses
ByteArrayOutputStream, FileOutputStream, FilterOutputStream, ObjectOutputStream, OutputStream, PipedOutputStream
Character Streams: This is a layer on top of byte streams. They convert bytes (binary data) into characters and characters into bytes using character encoding.
All character stream classes come from Reader and Writer .
Reader - Direct Known Subclasses
BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader
Writer - Direct Known Subclasses
BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter
Byte streams and character streams use unbuffered I / O.
This means that every read or write request is processed directly by the underlying OS. This can make the program much less efficient, since each such request often causes 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 Streams:
Buffered input streams read data from a memory area known as a buffer ; The native input API is only called when the buffer is empty.
Similarly, buffered output streams write data to the buffer , and the native output API is called only when the buffer is full.
A program can convert an unbuffered stream to a buffered stream using the wrapping idiom, where the object of the unbuffered stream is passed to the constructor for the buffered stream class.
Example:
inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
There are 4 classes of buffered streams that are used to package unbuffered streams:
To create buffered byte streams, use the BufferedInputStream and BufferedOutputStream classes.
To create buffered character streams, use the BufferedReader and BufferedWriter classes.