我想保存一个连接数组列表,这些连接在文件中具有 Pairs 值,但它显示 NotSerializedException。
我还实现了可序列化接口。
我以为它发生在双人班上。如果您有任何解决方案可以保存这些连接数组并从文件中检索。
连接.kt
data class Connection(
// @Transient
var firstPair: Pair<CircuitComponent, Int>,
// @Transient
var secondPair: Pair<CircuitComponent, Int>
) :Serializable{
var pathOfLine: NamedPath = NamedPath("")
data class State(
val firstComponentCode: String,
val secondComponentCode: String,
val firstTouchPointIndex: Int,
val secondTouchPointIndex: Int,
val pathName: String,
): Serializable
fun getState() = State(
firstPair.first.getCode(),
secondPair.first.getCode(),
firstPair.second,
secondPair.second,
pathOfLine.name
)
}
保存连接的数组列表
val projectsDirectory = File("${requireActivity().filesDir}/projects")
projectsDirectory.mkdir()
val thisProjectDirectory = File("${projectsDirectory}/${args.project.customerName}")
thisProjectDirectory.mkdir()
val file = File(thisProjectDirectory, "/image.jpg")
val out = FileOutputStream(file)
args.project.image = file.absolutePath
args.project.dateModified = SimpleDateFormat("MMMM dd yyyy", Locale.getDefault()).format(Date())
args.project.size = "${thisProjectDirectory.length() / 1000} kb"
serialize(thisProjectDirectory.absolutePath,"list_con.ser", connectionsList)
designBitmap.compress(Bitmap.CompressFormat.JPEG, 50, out)
out.close()
序列化方法
fun serialize(location:String,fileName:String,obj:Any){
val fileOutputStream = FileOutputStream("$location/$fileName")
val objectOutputStream = ObjectOutputStream(fileOutputStream)
objectOutputStream.writeObject(obj)
objectOutputStream.close()
}
获取连接
val projectLocation = "${requireActivity().filesDir}/projects/${args.project.customerName}"
val projectDirectory = File(projectLocation)
val connectionsList= deserialize(File(projectDirectory , "list_con.ser"))
反序列化
fun deserialize(file: File): Any? {
val fileInputStream = FileInputStream(file)
val objectInputStream = ObjectInputStream(fileInputStream)
return objectInputStream.readObject()
}