Java - SequenceInputStream close() method



Description

The Java 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 - Usage of SequenceInputStream close() method

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

SequenceInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

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();
      }
   }
}

Output

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

HelloWorld
Closing Streams...
Streams Closed.

Example - Properly closing a SequenceInputStream after reading

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

SequenceInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.SequenceInputStream;
import java.io.IOException;

public class SequenceInputStreamDemo {
   public static void main(String[] args) {
      try {
         ByteArrayInputStream input1 = new ByteArrayInputStream("Java".getBytes());
         ByteArrayInputStream input2 = new ByteArrayInputStream("Stream".getBytes());

         SequenceInputStream sis = new SequenceInputStream(input1, input2);

         int data;
         while ((data = sis.read()) != -1) {
            System.out.print((char) data);
         }

         sis.close(); // Closes SequenceInputStream and both ByteArrayInputStreams
         System.out.println("\nStream closed successfully.");

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

Output

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

JavaStream
Stream closed successfully.

Explanation

  • The stream reads from both sources ("JavaStream").

  • After reading,close() is called to release all underlying resources.

  • This is good practice in all I/O code.

Example - Attempting to read after closing the stream

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

SequenceInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.SequenceInputStream;
import java.io.IOException;

public class SequenceInputStreamDemo {
   public static void main(String[] args) {
      try {
         ByteArrayInputStream input1 = new ByteArrayInputStream("ABC".getBytes());
         ByteArrayInputStream input2 = new ByteArrayInputStream("DEF".getBytes());

         SequenceInputStream sis = new SequenceInputStream(input1, input2);

         sis.close(); // Close the stream immediately

         // Try to read after closing
         int data = sis.read(); // This should throw an exception
         System.out.println("Data: " + (char) data);

      } catch (IOException e) {
         System.out.println("Exception: Cannot read after closing the stream.");
      }
   }
}

Output

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

Data: ?

Explanation

  • The SequenceInputStream is closed before any reading.

  • A read attempt afterward causes an IOException.

java_io_sequenceinputstream.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy