Java.io.SequenceInputStream.close() Method

Advertisements


Description

The java.io.SequenceInputStream.close() method closes this input stream and releases any system resources associated with the stream. A closed SequenceInputStream cannot perform input operations and cannot be reopened.

Declaration

Following is the declaration for java.io.SequenceInputStream.close() method

public void close()

Parameters

  • NA

Return Value

This method does not return a value.

Exception

  • IOException -- if an I/O error occurs.

Example

The following example shows the usage of java.io.SequenceInputStream.close() method.

package com.tutorialspoint;

import java.io.*;

public class SequenceInputStreamDemo {

   public static void main(String[] args) {

      // create two  new strings with 5 characters each
      String s1 = "Hello";
      String s2 = "World";

      // create 2 input streams
      byte[] b1 = s1.getBytes();
      byte[] b2 = s2.getBytes();
      ByteArrayInputStream is1 = new ByteArrayInputStream(b1);
      ByteArrayInputStream is2 = new ByteArrayInputStream(b2);

      // create a new Sequence Input Stream
      SequenceInputStream sis = new SequenceInputStream(is1, is2);
      try {
         // read 10 characters, 5 from each stream
         for (int i = 0; i < 10; i++) {
            char c = (char) sis.read();
            System.out.print("" + c);
         }

         // change line
         System.out.println();

         // start closing the streams
         System.out.println("Closing Streams...");

         // close the streams
         sis.close();
         is1.close();
         is2.close();

         // print message for closed streams
         System.out.println("Streams Closed.");

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result:

HelloWorld
Closing Streams...
Streams Closed.



Advertisements
Advertisements