Introduction to Streams in Java

A stream can be considered as a sequence of bytes travelling from a source to a destination.

All input and output data transfer in Java happens through streams.

There can be different types of sources and destinations such as disk files, input devices like keyboards and even other programs, and there are different stream classes available corresponding to each of these types of sources and destinations.

All stream classes are present in the in the java.io package.

 

Classification of streams

Input and output streams

  • Based on the direction of data flow, streams can be categorized as input streams and output streams. 

 

Byte and character streams

Based on the type of data we can classify streams as byte streams and text streams (aka character streams).

  • Byte streams

    • represent data in the form of bytes.

    • All byte stream classes are descended from InputStream and OutputStream.

    • Class names of byte streams end with the word stream as in FileInputStream and FileOutputStream.

  • Text streams (or character streams)

    • represent data as characters.

    • All character stream classes are descended from Reader and Writer.

    • Class names of text streams end with the word ‘Reader’ or ‘Writer’ as in FileReader and FileWriter.

    • Java stores character values using Unicode conventions. Text streams automatically convert the Unicode internal format to and from the local character set.

 

Wrapper streams

  • One stream may wrap around another stream to add some functionality to the inner stream or to make the inner stream do some low level tasks.

  • Filter streams wrap around other streams and transform the data providing additional functionality.

  • Subclasses of FilterInputStream are BufferedInputStream, CheckedInputStream, CipherInputStream, DataInputStream, DeflaterInputStream, DigestInputStream, InflaterInputStream, LineNumberInputStream, ProgressMonitorInputStream and PushbackInputStream.

  • Subclasses of FilterOutputStream are BufferedOutputStream, CheckedOutputStream, CipherOutputStream, DataOutputStream, DeflaterOutputStream, DigestOutputStream and InflaterOutputStream.

 

To improve efficiency we can use Buffered classes in connection with other streams

  • BufferedOutputStream can be used along with FileOutputStream to write data to a file. The characters will not be directly written to the file, but to the buffer. When the buffer is full, then the FileOutputStream will write the entire buffer in a single step into the file.

  • Similarly we can use BufferedInputStream to read a buffer full of data at a time from the file.

  • Another example is DataInputStream; we can attach the keyboard to a data input stream as: 

    • DataInputStream dis=new DataInputStream(System.in);.

 

Stream variables in System class

  • java.lang.System class’s static member variable ‘in’ (System.in) is of type PrintStream and represents the standard input device that is keyboard by default.

  • System.out of type PrintStream and System.err of type InputStream, both are used to display messages on the default output device, which is monitor.

    • System.out is used to display normal messages whereas System.err is used to display error messages.

 

Text streams as wrappers for byte streams

  • Text streams often act as wrappers for byte streams; the byte streams perform the low level physical IO operations, and text streams handle the translation between characters and bytes.

  • Many readers cannot wrap around a byte stream. InputStreamReader can be useful in such cases as it can accept byte streams and act as a bridge between character streams and byte streams.

Example:

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter the filename”);

String fname=br.readLine();

This code will print "Enter the filename" to the console and then read a line from the console. 

 

Markable streams

Markable streams provide the capability to mark a position in the stream and then later reset the stream so that it can be reread from the marked position.

  • The markSupported() method returns true if the stream is markable.

  • The mark() method marks a position in the stream and it takes the buffer capacity as an integer parameter.

    • The buffer memory is used to keep track of the data between the mark and the current position and when this buffer memory is exceeded, the mark becomes invalid.

  • The reset() method simply repositions the stream to its last marked position.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)