Skip to content

Instantly share code, notes, and snippets.

@shijinkui
Last active August 29, 2015 14:08
Show Gist options
  • Save shijinkui/a5dcbc1bd7a3130bab34 to your computer and use it in GitHub Desktop.
Save shijinkui/a5dcbc1bd7a3130bab34 to your computer and use it in GitHub Desktop.
package sample
import java.io._
/**
* 序列化测试
* Created by 玄畅 on 29/10/14.
*
* 结果:
* 序列化之前:
* transient object TTT is not serialized: false|xxxx
* 序列化之后:
* transient object TTT is not serialized: true|xxxx
*/
object ScalaSerialTest {
def main(args: Array[String]) {
val t1 = new TTT(1212, "xxxx")
val t = new T2(t1, "xxxx")
println("序列化之前:")
println(t.toString)
val f = "/tmp/a.bin"
val fos: OutputStream = new FileOutputStream(f)
val oos: ObjectOutputStream = new ObjectOutputStream(fos)
oos.writeObject(t)
oos.close()
val fis: InputStream = new FileInputStream(f)
val ois: ObjectInputStream = new ObjectInputStream(fis)
val tt = ois.readObject
println("序列化之后:")
println(tt.toString)
ois.close()
}
}
class T2(@transient val t: TTT, val str: String) extends Serializable {
override def toString = "transient object TTT is not serialized: " + ( t == null ) + "|" + str
}
class TTT(@transient val number: Int, val str: String) extends Serializable {
override def toString = number + "||" + str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment