Java的NotSerializableException 示例
Java中的序列化是一种将对象的状态写入字节流的机制。它主要用于 Hibernate、RMI、JPA、EJB 和 JMS 技术。
序列化的逆向操作称为反序列化,其中将字节流转换为对象。序列化和反序列化过程与平台无关,这意味着您可以在一个平台上序列化一个对象,然后在不同的平台上反序列化它。
在Java,当类的实例必须实现 Serializable 接口时,会抛出 NotSerializableException 异常。异常由序列化运行时或类的实例引发。 NotSerializableException 的参数是类的名称。
NotSerializableException 类扩展了 ObjectStreamException 类,该类被定义为特定于 Object Stream 类的所有异常的超类。此外,ObjectStreamException 类扩展了 IOException,它表示发生了 I/O 异常。
插图:
java.io
Class NotSerializableException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.io.IOException
java.io.ObjectStreamException
java.io.NotSerializableException
Note: All Implemented Interfaces are Serializable interface.
句法:
public class NotSerializableException
extends ObjectStreamException
在开始之前让我们讨论这个类的构造函数
- NotSerializableException():构造一个 NotSerializableException 对象。
- NotSerializableException(String classname):构造一个带有消息字符串的 NotSerializableException 对象。
示例 1:
Java
// Java Program to Illustrate NotSerializableException
// Where Exception Is Thrown
// Importing required classes
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
// Class 1
// Helper class
class Employee {
// Member variables
private String id;
// Member methods
// Method 1
// To get ID of an employee
public String getId() { return id; }
// Method 1
// To set ID of an employee
public void setId(String id)
{
// this keyword refers to current object itself
this.id = id;
}
}
// Class 2
// Main Class
public class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Create FileOutputStream class object to
// create a file
FileOutputStream out
= new FileOutputStream("employee.dat");
// Similarly creating ObjectOutputStream class
// object
ObjectOutputStream outputStream
= new ObjectOutputStream(out);
// Creating objects of class 1
Employee obj = new Employee();
// Assifning ID to an employee
obj.setId("001");
// Writing objects to stream
outputStream.writeObject(obj);
// Good practice is always to
// Close the stream using close() method
outputStream.close();
}
}
Java
// Java Program to Illustrate NotSerializableException
// where No Exception is Thrown Using Serializable interface
// Importing input output class
import java.io.Serializable;
// By implementing Serializable interface
// we are allowing Student object to
// be stored in TestFile.txt
// Class 1
// Helper class extending to Serializable interface
class Student implements Serializable {
// Member variables of this class
int id;
String name;
// Constructor of ths class
public Student(int id, String name)
{
this.id = id;
this.name = name;
}
}
// Class 2
// Main class
class Persist {
// Main driver method
public static void main(String args[])
{
// try block to check for exceptions
try {
// Creating the object
Student s1 = new Student(007, "Test");
// Creating stream and writing the object
FileOutputStream fout
= new FileOutputStream("TestFile.txt");
ObjectOutputStream out
= new ObjectOutputStream(fout);
out.writeObject(s1);
out.flush();
// Closing the stream to free up memory space
// using close() method
out.close();
// Display command to shown proper execution of
// a program
System.out.println(
"Object stored successfully");
}
// Catch block to handle the exceptions
catch (Exception e) {
// Print and display the exception on the
// console
System.out.println(e);
}
}
}
输出 :
Errors in Code
Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "employee.dat" "write")
at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.base/java.security.AccessController.checkPermission(AccessController.java:897)
at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:322)
at java.base/java.lang.SecurityManager.checkWrite(SecurityManager.java:752)
at java.base/java.io.FileOutputStream.(FileOutputStream.java:225)
at java.base/java.io.FileOutputStream.(FileOutputStream.java:126)
at NotSerializableExceptionExample.main(NotSerializableExceptionExample.java:21)
如何处理 NotSerializableException
- 最简单的解决方案是找到引发异常的类并使其实现 Serializable 接口。但是,如果抛出异常的类属于第三方库,这可能不可行。
- 如果类引用了不可序列化的对象并且这些对象不应该被序列化,那么您可以将这些对象声明为瞬态对象。一旦类的字段被声明为瞬态,它就会被可序列化运行时忽略。
示例 2:
Java
// Java Program to Illustrate NotSerializableException
// where No Exception is Thrown Using Serializable interface
// Importing input output class
import java.io.Serializable;
// By implementing Serializable interface
// we are allowing Student object to
// be stored in TestFile.txt
// Class 1
// Helper class extending to Serializable interface
class Student implements Serializable {
// Member variables of this class
int id;
String name;
// Constructor of ths class
public Student(int id, String name)
{
this.id = id;
this.name = name;
}
}
// Class 2
// Main class
class Persist {
// Main driver method
public static void main(String args[])
{
// try block to check for exceptions
try {
// Creating the object
Student s1 = new Student(007, "Test");
// Creating stream and writing the object
FileOutputStream fout
= new FileOutputStream("TestFile.txt");
ObjectOutputStream out
= new ObjectOutputStream(fout);
out.writeObject(s1);
out.flush();
// Closing the stream to free up memory space
// using close() method
out.close();
// Display command to shown proper execution of
// a program
System.out.println(
"Object stored successfully");
}
// Catch block to handle the exceptions
catch (Exception e) {
// Print and display the exception on the
// console
System.out.println(e);
}
}
}
输出:
Object stored successfully