JAVA序列化的两种方式【转贴】
JAVA序列化的两种方式
//大家都知道Serializable是一个mark interface,告诉JVM这个对象可以被转换成二进制流来传输.
//Serializable 在我们实现这个接口的时候,我们可以使用4个私有方法来控制序列化的过程:
//我们来看一个例子:
public class FooImpl implements java.io.Serializable
{
private String message;
public String getFoo()
{
return message;
}
public void setMessage( String message )
{
this.message = message;
}
private void writeObject( java.io.ObjectOutputStream out ) throws IOException
{
System.out.println( "writeObject invoked" );
out.writeObject( this.message == null ? "hohohahaha" : this.message );
}
private void readObject( java.io.ObjectInputStream in ) throws IOException, ClassNotFoundException
{
System.out.println( "readObject invoked" );
this.message = (String) in.readObject();
}
private Object writeReplace() throws ObjectStreamException
{
System.out.println( "writeReplace invoked" );
return this;
}
private Object readResolve() throws ObjectStreamException
{
System.out.println( "readResolve invoked" );
return this;
}
public Object serialize() throws IOException, ClassNotFoundException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( this );
ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() );
ObjectInputStream ois = new ObjectInputStream( bais );
return ois.readObject();
}
public static void main(String[] args) throws IOException, ClassNotFoundException
{
FooImpl fooimpl = new FooImpl();
fooimpl.se
相关文档:
变量是Java 程序的一个基本存储单元。变量由一个标识符,类型及一个可选初始值的组合定义。此外,所有的变量都有一个作用域,定义变量的可见性,生存期。
声明一个变量
一、 静态初始化
在Java 中,所有的变量必须先声明再使用。基本的变量声明方 ......
/*
提供zip文件的解压缩接口:
AdapterZipFile:
输入:zipFileName(zip文件的绝对路径),outputDirectory(zip文件解压缩后的存放路径)
输出:
说明:初始化函数
unZipFile:
输入:无
输出:
说明:解压缩zip文件,解压缩 ......
import java.lang.reflect.Array;
public class ReflectionTest {
public static void main(String[] args) {
try {
Example obj = new Example();
j ......
/**
* ==号是比较两个基本类型是否相等,或者比较两个对象引用是否相同
*/
public class T {
public static void main(String[] args) {
Integer i1 = 128;
Integer i2 = 128;
int i3 = 128;
int i4 = 128;
Integer i5 = 127;
Integer i6 = 127; ......