Java I/O streams; what are the differences?
Solution 1
This is a big topic! I would recommend that you begin by reading I/O Streams:
An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.
Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.
Solution 2
Streams: one byte at a time. Good for binary data.
Readers/Writers: one character at a time. Good for text data.
Anything "Buffered": many bytes/characters at a time. Good almost all the time.
Solution 3
When learning Java I made this mental scheme about java.io:
Streams
- byte oriented stream (8 bit)
- good for binary data such as a Java .class file
- good for "machine-oriented" data
Readers/Writers
- char (utf-16) oriented stream (16 bit)
- good for text such as a Java source
- good for "human-oriented" data
Buffered
- always useful unless proven otherwise
Solution 4
Separate each name into words: each capital is a different word.
- File Input Stream is to get Input from a File using a Stream.
- File Output Stream is to write Output to a File using a Stream
And so on and so forth
As mmyers wrote :
Streams: one byte at a time.
Readers/Writers: one character at a time.
Buffered*: many bytes/characters at a time.
Solution 5
The specialisations you mention are specific types used to provide a standard interface to a variety of data sources. For example, a FileInputStream and an ObjectInputStream will both implement the InputStream interface, but will operate on Files and Objects respectively.
Related videos on Youtube
Comments
-
Lawrence about 2 years
java.io
has many different I/O streams, (FileInputStream, FileOutputStream, FileReader, FileWriter, BufferedStreams... etc.) and I am confused in determining the differences between them. What are some examples where one stream type is preferred over another, and what are the real differences between them? -
palantus almost 15 yearsReally? The most recent time I checked, I got about a 40% performance drop when switching to a plain FileReader. What are you reading from?