📜  Java的StreamCorruptedException

📅  最后修改于: 2022-05-13 01:55:04.560000             🧑  作者: Mango

Java的StreamCorruptedException

ObjectStreamException 的StreamCorruptedException类是当从对象流中读取的控制信息违反内部一致性检查时抛出的异常。它将创建一个 StreamCorruptedException 并列出抛出异常的原因。如果在构造函数中没有传递参数,那么它将创建一个 StreamCorruptedException 并且不列出抛出异常的原因。

句法:

public StreamCorruptedException(String reason)

参数: reason – 描述异常原因的“字符串”

示例 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
            // StreamCorruptException occurs
            System.out.println(e);
        }
 
        // Catch block 2
        // Handling bsic I/O exception
        catch (Exception e) {
 
            // If EOF exception or any other exception
            // occurs
            System.out.println(e);
        }
    }
}


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 fo 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 Occured");
        }
        // Catch block 2
        // Handling basic I/O exceptions
        catch (Exception e) {
 
            // Print statement
            System.out.println("Hello");
        }
    }
}


输出:

java.io.StreamCorruptedException: File format not recognised

现在专注于识别文件格式的第二个示例。在实施之前,请阅读下面的示例说明如下。

示例 2:

Java

’ 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

输出:

// 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 fo 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 Occured");
        }
        // Catch block 2
        // Handling basic I/O exceptions
        catch (Exception e) {
 
            // Print statement
            System.out.println("Hello");
        }
    }
}