Stream is basically sequence of data. To write or read a file we need to use Streams. Because we can’t read or write files directly. So we have to read or write files in sequence. And that sequence is called as Stream.
Streams are of 2 types:
Input Stream
Output Stream
Input Stream: This Stream is used to perform read operation on Stream. Like reading a file, reading input from keyboard, reading data from internet etc.
Output Stream: This is opposite to Input Stream, as its name clearly explains. Output Stream is used to perform write operations on Stream. For example writing into file, writing something on screen, writing data to Url (on internet).
Java provides strong and flexible support for Input/Output Stream related to files and Network.
Java Byte Stream are used o perform input/output operations of 8bit data. And Character Stream is used to perform input/output operations of 16bit unicode data. There are many classes related to Character Stream like FileReader, FileWriter etc. FileReader use FileInputStream internally and FileWriteruse FileOutputStream.
import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class CopyCharacters {public static void main(String[] args) throws IOException {FileReader inputStream = null;FileWriter outputStream = null;try {inputStream = new FileReader("xanadu.txt");outputStream = new FileWriter("characteroutput.txt");int c;while ((c = inputStream.read()) != -1) {outputStream.write(c);}} finally {if (inputStream != null) {inputStream.close();}if (outputStream != null) {outputStream.close();}}}}
These Streams are used to perform Standard operation like reading input from keyboard,mouse and Writing output to monitor etc.
This Stream handle input from user using Keyboard,mouse etc. General class for this Stream is System.in.
class Java{public static void main(String[] args){//get input from userScanner sc = new Scanner(System.in);//this will print output on console window.System.out.println("Printing the line passed in:");while(sc.hasNextLine()) System.out.println(sc.nextLine());}}
This Stream handle output to user using monitor/display. General class for this Stream is System.out.
class Java{public static void main(String[] args){//this will print output on console window.System.out.println("Hello from Learn Pain Less");}}
This Stream is used to display Error to users, this is also displayed on monitor/screen. General class for this Stream is System.err.
class Java{public static void main(String[] args){//this will print output on console window.System.err.println("Printing Error from Learn Pain Less");}}
As described earlier that Stream is sequence of data. And Input/Output Stream are used to perform Read and Write operations respectively. There is another class which is also used to Read and Write Stream. That classes are FileInputStream and FileOutputStream.
FileInputStream is used to read data from file. To use FileInputStream we will use its default constructor and pass path to that file which we want to read. And then we can read file in sequence.
InputStream fs = new FileInputStream ("java/android/test.txt");fs.read();
FileOutputStream is used to write data into file. This is same as FileInputStream Stream.
OutputStream os = new FileOutputStream ("java/android/a.txt");
Quick Links
Legal Stuff
Social Media