StreamCorruptedException in Java


In java, StreamCorruptedException occurs when problem is found with the data being read from or written to a stream. The problem may be the data we are reading or writing is in the wrong format or contains errors. If the format of given file is not correct. When stream has been closed unexpectedly or the data has been partially overwritten.

In this article, we will talk about StreamCorruptedException with examples. But before moving on it is important to understand streams and few of its method that we are going to use in examples.

Program to handle StreamCorruptedException

Streams

Stream is an abstraction that is used while performing Input and Output operations in java. Basically, the input streams are used to take input from sources like keyboard, disk files etc. The output streams refer to the destination where data gets displayed or written.

Classes

FileReader − It is used for reading characters.

FileWriter − It is used to write characters.

To use these classes we need to define their objects −

Syntax

FileReader nameOfobject = new FileReader(“nameOfFile”); 
FileWriter nameOfobject = new FileWriter(“nameOfFile”);

StreamCorruptedException − It is a sub class of ObjectStreamException. It can be used in two ways, without providing a reason for exception and with a reason enclosed in double quotes of type string.

Syntax

StreamCorruptedException();
Or,
StreamCorruptedException(“value”);

Methods

read() − It is used to read a byte from given source.

To work with I/O Streams we need to import the following package in our java program −

import java.io.*;

‘*’ signifies that we are importing all the classes available in this package. StreamCorruptedException is one of the classes of java.io.

Example 1

In this example, we will use StreamCorruptedException class without giving the reason for exception.

import java.io.*;
public class Streams {
   public static void main(String args[]) throws IOException {
      FileReader reading = null;
      FileWriter writing = null;
      try {
         reading = new FileReader("Stream.java");
         writing = new FileWriter("stream");
         int copy;
         while ((copy = reading.read()) != -1) {
            throw new StreamCorruptedException();
         }
      }   
      catch (StreamCorruptedException exp) {
         // To handle StreamCorruptedException 
         System.out.println(exp);
      }
      catch (Exception exp) {
         // To handle other exception
         System.out.println("Other exception found");
      }
   }
}

To compile the above java file: ‘javac Streams.java’

After compilation run the java file by using following command: ‘java Streams’

Output

Other exception found

In the above code, we have created two instances ‘reading’ and ‘writing’ of class FileReader and FileWriter respectively to perform read and write operations to the given file. In the while loop of try block, we have tried to read the content of file ‘Stream.java’. Although we have created a variable ‘copy’ of type integer to store the content of that file but we haven’t given instructions on where to copy that’s why StreamCorruptedException had thrown.

Example 2

In this example, we will use StreamCorruptedException class with the reason for exception.

import java.io.*;
public class Streams {
   public static void main(String args[]) throws IOException {
      FileReader reading = null;
      FileWriter writing = null;
      try {
         reading = new FileReader("Stream.java");
         writing = new FileWriter("stream");
         int copy;
         while ((copy = reading.read()) != -1) {
            throw new StreamCorruptedException("Please!! Provide a valid file type!");
         }
      }   
      catch (StreamCorruptedException exp) {
         // To handle StreamCorruptedException 
         System.out.println(exp);
      }
      catch (Exception exp) {
         // To handle other exception
         System.out.println("Other exception found");
      }
   }
}

Output

java.io.StreamCorruptedException: Please!! Provide a valid file type!

In the above code, we have used the example 1 program but this time we have used a message with thrown exception that shows the reason for occurred exception.

Conclusion

In java, we handle an exception using try and catch block. The exception occurs at runtime not during compilation that’s why it is important to handle it properly otherwise it may cause trouble for us. In this article, we have understood how to handle one of the exception in java named StreamCorruptedException.

Updated on: 12-May-2023

406 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements