Java 中的序列化是将对象的状态写入byte-stream 的机制。主要应用于Hibernate、RMI、JPA、EJB、JMS技术。
序列化的逆操作称为反序列化,其中byte-stream被转换为对象。序列化和反序列化过程是平台无关的,这意味着您可以在一个平台上序列化对象,并在不同平台上反序列化它。
在 Java 中,当类的实例必须实现 Serialized 接口时,会引发 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对象。
- NotSerializedException(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();
}
}
输出:
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.<init>(FileOutputStream.java:225) at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:126) at NotSerializableExceptionExample.main(NotSerializableExceptionExample.java:21)
如何处理NotSerializableException
- 最简单的解决方案是找到抛出异常的类并使其实现 Serialized 接口。但是,如果引发异常的类属于第三方库,则这可能不可行。
- 如果类引用不可序列化的对象并且这些对象不应被序列化,则可以将这些对象声明为瞬态对象。一旦类的字段被声明为瞬态,那么它就会被可序列化运行时忽略。
示例 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 this 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
相关用法
- Java Number byteValue()用法及代码示例
- Java Number doubleValue()用法及代码示例
- Java Number floatValue()用法及代码示例
- Java Number intValue()用法及代码示例
- Java Number longValue()用法及代码示例
- Java Number shortValue()用法及代码示例
- Java NavigableMap ceilingEntry()用法及代码示例
- Java NavigableMap ceilingKey()用法及代码示例
- Java NavigableMap clear()用法及代码示例
- Java NavigableMap firstEntry()用法及代码示例
- Java NavigableMap floorEntry()用法及代码示例
- Java NavigableMap floorKey()用法及代码示例
- Java NavigableMap headMap()用法及代码示例
- Java NavigableMap higherEntry()用法及代码示例
- Java NavigableMap higherKey()用法及代码示例
- Java NavigableMap isEmpty()用法及代码示例
- Java NavigableMap lastEntry()用法及代码示例
- Java NavigableMap lowerEntry()用法及代码示例
- Java NavigableMap lowerKey()用法及代码示例
- Java NavigableMap pollFirstEntry()用法及代码示例
- Java NavigableMap pollLastEntry()用法及代码示例
- Java NavigableMap put()用法及代码示例
- Java NavigableMap size()用法及代码示例
- Java NavigableSet add()用法及代码示例
- Java NavigableSet addAll()用法及代码示例
注:本文由纯净天空筛选整理自praveen13kulkarni大神的英文原创作品 NotSerializableException in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。