Predefine Stream:- As you all know, all java programs automatically import the java.lang package. This package defines a class called System, it contains 3 predefined stream variables, called in, out, and err. These fields are declared as public and static within System. This means they can be used by any other part of program and without reference to a specific System Object. System.out refers to Standard output stream which is bydefault console . System.in refers to standard input which is by default Keyborad. System.err refers to standard error stream, which is also the console by default.
- Flow of data from primary memory to secondary memory and vice-versa is known as Stream.
- Based on amount of data being transferred between primary memory and secondary memory, in java we have two types of Streams. They are:
- Byte Stream – 1 byte of data will be transmitted at a time (Low level Application)
- Character Stream – 2 bytes of data will be transmitted at a time (Higher Level application Internet Programming)
- Byte Stream: Byte Streams are those in which 1 byte of data will be transferred at a time between main memory and secondary memory and vice versa.
- Hierarchy chart of Byte Stream:
- Character Stream:-
Java Byte streams are used to perform input and output of 8-bit bytes, where as Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are FileReader and FileWriter. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.
Reader is used for input and Writer is used for output. They both are abstract classes.