Java I/O streams; what are the differences?
Asked Answered
D

7

24

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?

Divide answered 21/7, 2009 at 15:44 Comment(1)
This is probably the most thorough overview of the various streams, Reader's and Writer's in the Java IO API: tutorials.jenkov.com/java-io/overview.html It's part of a larger Java IO tutorial covering both byte and charater based streams. It also covers streams that are used for reading and writing raw numeric data, like int's float's etc. It also covers streams used for parsing like the PushbackInputStream and the PushbackReader.Panhandle
T
13

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.

Tankoos answered 21/7, 2009 at 15:47 Comment(0)
M
42

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.

Mcdougall answered 21/7, 2009 at 15:47 Comment(2)
I disagree that using a buffered stream is good "almost all the time". In fact, it's useless overhead more often than not in my experience.Chromatics
Really? The most recent time I checked, I got about a 40% performance drop when switching to a plain FileReader. What are you reading from?Mcdougall
B
28

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
Breadroot answered 21/7, 2009 at 15:55 Comment(0)
T
13

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.

Tankoos answered 21/7, 2009 at 15:47 Comment(0)
K
8

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.

Koralie answered 21/7, 2009 at 15:52 Comment(0)
D
3

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.

Diffractive answered 21/7, 2009 at 15:48 Comment(0)
P
3

Java input and output is defined in terms of an abstract concept called a “stream”, which is a sequence of data. There are 2 kinds of streams.

  • Byte streams (8 bit bytes) Æ Abstract classes are: InputStream and OutputStream
  • Character streams (16 bit UNICODE) Æ Abstract classes are: Reader and Writer

java.io.* classes use the decorator design pattern. The decorator design pattern attaches responsibilities to objects at runtime. Decorators are more flexible than inheritance because the inheritance attaches responsibility to classes at compile time. The java.io.* classes use the decorator pattern to construct different combinations of behavior at runtime based on some basic classes. enter image description hereenter image description here

from the book Java/J2EE Job Interview Companion By K.Arulkumaran & A.Sivayini

Perrie answered 24/11, 2017 at 10:56 Comment(0)
H
2

Byte streams are mostly and widely used stream type in java 1.0 for both character and for byte. After java 1.0 it was deprecated and character streams plays a important role. ie., for example

BufferedReader will get the character from the source, and its constructor looks like BufferedReader(Reader inputReader)..

Here Reader is an abstract class and the once of its concrete classes are InputStreamReader, which will converts bytes into characters and take input from the keyboard(System.in)...

BufferedReader : Contains internal Buffer that will read characters from the stream. Internal counter keeps track of next character to be supplied to the buffer thru read(). InputStreamReader will takes input as bytes and converts internally into characters.

Hyracoid answered 10/11, 2009 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.