StreamCorruptedException in Java
The class StreamCorruptedException of ObjectStreamException is an exception thrown when control information that was read from an object stream violates internal consistency checks. It will create a StreamCorruptedException and list a reason why the exception was thrown. If no parameters are passed in the constructor, then it will create a StreamCorruptedException and list no reason why the exception was thrown.
Syntax:
public StreamCorruptedException(String reason)
Parameters: reason – “String” describing the reason for the exception
Example 1:
Java
// Java program to Illustrate StreamCorruptedException // Importing required classes import java.io.*; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StreamCorruptedException; // Main class class GFG { // Main driver method public static void main(String[] args) throws Exception { // Creating an object of InputStream class InputStream in = new FileInputStream( "testout.txt" ); // Representing input object in form of string DataInputStream dis = new DataInputStream(in); // Writing the data // Try block to check if exception occurs try { // readByte will read single byte from the file if (dis.readByte() != 1 ) { throw new StreamCorruptedException( "File format not recognised" ); // If file contains NULL in first byte // then exception will be thrown } } // Catch block 1 // Handling stream corrupted exception catch (StreamCorruptedException e) { // Display message on console if // StreamCorruptedException occurs System.out.println(e); } // Catch block 2 // Handling basic I/O exception catch (Exception e) { // If EOF exception or any other exception // occurs System.out.println(e); } } } |
Output:
java.io.StreamCorruptedException: File format not recognised
Now dwelling on the second example where file format is recognized. It is as follows for which go through below sample note below prior to implementation.
Note: Make the C098.txt file in the same folder of the program and copy the following fragment as it is. It is an example corrupted file
C098.txt
’ sr Product L desct Ljava / lang / String; L priceq ~ L productIdq ~ xpt Bookt Rs .200t P001’ sr Product L desct Ljava / lang / String; L priceq ~ L productIdq ~ xpt Laptopt Rs .45, 500t P087
Example 2:
Java
// Java program to Illustrate StreamCorruptedException // Importing required classes import java.io.*; import java.util.*; // Main class // To illustrate object stream exception class GFG { // Main driver method public static void main(String args[]) throws Exception { // Text in file is stored as a string String CId = "C098.txt" ; File objFile1 = new File(CId); // Try block to check for exception try { // Creating an object of FileInputStream class FileInputStream objFIS = new FileInputStream(objFile1); // Creating an object of ObjectInputStream class ObjectInputStream objI = new ObjectInputStream(objFIS); Object obj = null ; } // Catch block 1 // Handling stream corrupted exception catch (StreamCorruptedException ex) { // Display this message as exception is caught System.out.println( "Stream Corrupted Exception Occurred" ); } // Catch block 2 // Handling basic I/O exceptions catch (Exception e) { // Print statement System.out.println( "Hello" ); } } } |
Output:
Stream Corrupted Exception Occurred
Contact Us