^

Streams

A stream is just a continuous flow of data between source (from which is being read) and destination (to which the data is being written) e n network connection/socket etc. In Java, IO streams are typically byte based. It means that you can either read bytes from or write bytes to a stream.
Diagram below shows how the data can be read from source and can be read from source and can be written to destination.
Figure-1

Using Stream classes through java program data can be read from the many Sources and can be written to many other destinations as shown below:
Figure - 2
Basically, stream is an object of any stream class used to read data from source and write on destination . A stream used for reading is called input stream and used for writing is known as output stream.
Types of Streams
  1. Byte Streams
  2. Character Streams
In addition to dealing with bytes and characters, streams are provided for input and output of the primitive values and objects also.

Byte Streams: Input Streams and Output Streams (8 bits)

Java.io package provides an inheritance hierarchy classes to work with byte streams. The abstract classes: InputStream and OutputStream are the root of the inheritance hierarchies for handling bytes as shown follows:
Partial Byte Input Stream Inheritance Hierarchy
Figure-3
Partial Byte Output Stream Inheritance Hierarchy
Figure-4

InputStream & OutputStream Classes

InputStream Class
It is an abstract class consisting of the following commonly used methods: -
  1. int read () throws IOException
  2. int read (byte [] array) throws IOException
  3. int read (byte [] array, int startIndex, int N) throws IOException.
The first, read() method reads a byte, but it returns an integer value. The byte od is stored in the least significant bits of the int value and its remaining bits are out to zero. It returns -1, when end of the stream is found.
The second, overloaded form of read() method reads a group of bytes from streams. The number of bytes, group consists depends upon the length of array you passed as argument. The number of bytes actually read is returned an integer.
The third overloaded form of read() method reads N bytes from the stream and stores it on specified index position in the array, which is passed as argument.
OutputStream Class
Like InputStream class, the OutputStream class is also an abstract class. It consists of the following commonly used methods:
  1. void write (int) throws IOException
  2. void write (byte[] array) throw IOException.
  3. void write (byte[] array, int startIndex, int N) throws IOException.
The first write(int b) method writes the specified byte to the stream. It writes the eight low order bits of the argument b and the rest 24 high order bits are ignored
The second write (byte []array) method writes the b.length bytes from one specified byte array to the stream.
The third write (byte | array, int startIndex, int n) method writes N bytes from wie specified byte array starting from startIndex to the stream.
Other than write() methods, OutputStream class also consists the following methods:
  1. void close() throws IOException.
  2. void flush() throws IOException
The close() method closes the stream and also releases any system resources associated with this stream. Class InputStream also consists of the close() method.
The flush() method flushes the output stream and forces any buffered output bytes to be written.
FileInputStream Class
The FileInputStream class inherits InputStream class. This class obtaine bytes from a file in the file system.
Constructors
  1. FileInputStream (String fileName) throws FileNotFoundException
  2. FileInputStream (File fileObject) throws FileNotFoundException
  3. FileInputStream (FileDescriptor descObject) throws FileNotFoundException
The above constructor creates a FileInputStream object by opening a connection to an actual file, the file can be specified by its name (String), through a File object or using a FileDescriptor object. If the file does not exist or is a directory rather than a file or for some other reason cannot be opened for reading then FileNotFoundException is thrown.
FileOutputStream Class
The FileOutputStream class inherits OutputStream class. This class write bytes to the specified file through its constructor: Constructors
  1. FileOutputStream (String fileName) throws FileNotFoundException
  2. FileOutputStream (String fileName, boolean append) throws FileNotFoundException
  3. FileOutputStream (File fileObject) throws FileNotFoundException
  4. File OutputStream (FileDescriptor fdObject) throws FileNotFoundException
The file to be connected for writing bytes can be specified either by its name (string) or FileObject or by FileDescriptor object.
As FileInputStream class provides implementation for read() methods from InputStream class, similarly, the FileOutputStream class provides an implementation of write() methods.
Implementation
The following program copy the existing image (.jpg) file and creates a new image (.jpg) file with new name.
Before running the following program, assure that your current folder/ directory should have image file having name real.jpg. It will create a new image file with name java.jpg.
        //FileOutputStream.java
        import java.io.*;
        public class FileInOutStreams
        { public static void main(String[] args) throws Exception
        { copyImage();}
        static void copyImage()
        {
          File src=new File("real.jpg");
          read=new FileInputStream(src);
          File des=new File("java.jpg");
          write=new FileOutputStream(des);
          int c,count=0;
          while((c=read.read()!=-1))
          {
          write.write(c);
          count++;
          }
          System.out.print(count+"Byte Copied....");
        }
        catch(FileNotFoundException ex)
        {ex.printStackTrace();}
        catch(IOException ex)
        {ex.printStackTrace();}
        try
        {
        read.close();
        write.close();
        }
        catch(IOException ex){ex.printStackTrace();}
        }
       
       Output:
              Copied....
       

About the Author
Rajesh K. Bansal (SCJP-Sun Certified Java Programmer)
20 Years experience in Training & Development. Founder of realJavaOnline.com, loves coding in Java(J2SE, J2EE), C++,PHP, Python, AngularJS, Android,MERN Stack(MongoDB,Express,ReactJS,NodeJS). If you like tutorials and want to know more in depth about Java , buy his book "Real Java" available on amazon.in.
#Email : bcebti@gmail.com #Contact : 98722-46056
Available on Amazon
Card image cap
Under the guidance of Founder & Author of "realJavaOnline.com". M:9872246056
Card image cap