序列化、Socket、邮件、多线程

一、序列化

对象

public class Employee implements java.io.Serializable
{
    public String name;
    public String address;
    public transient int SSN;
    public int number;
    public void mailCheck()
    {
        System.out.println("Mailing a check to " + name
                + " " + address);
    }
}

序列化

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;

public class 反序列化 {
    public static void main(String [] args)
    {
        Employee e = null;
        try
        {
            // 创建输入的文件对象
            FileInputStream fileIn = new FileInputStream("./src/序列化、网络编程、多线程、邮件/employee.ser");
            // 创建输入流对象
            ObjectInputStream in = new ObjectInputStream(fileIn);
            e = (Employee) in.readObject();
            in.close();
            fileIn.close();
        }catch(IOException i)
        {
            i.printStackTrace();
            return;
        }catch(ClassNotFoundException c)
        {
            System.out.println("Employee class not found");
            c.printStackTrace();
            return;
        }
        System.out.println("Deserialized Employee...");
        System.out.println("Name: " + e.name);
        System.out.println("Address: " + e.address);
        System.out.println("SSN: " + e.SSN);
        System.out.println("Number: " + e.number);
    }
}

反序列化

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;

public class 反序列化 {
    public static void main(String [] args)
    {
        Employee e = null;
        try
        {
            // 创建输入的文件对象
            FileInputStream fileIn = new FileInputStream("./src/序列化、网络编程、多线程、邮件/employee.ser");
            // 创建输入流对象
            ObjectInputStream in = new ObjectInputStream(fileIn);
            e = (Employee) in.readObject();
            in.close();
            fileIn.close();
        }catch(IOException i)
        {
            i.printStackTrace();
            return;
        }catch(ClassNotFoundException c)
        {
            System.out.println("Employee class not found");
            c.printStackTrace();
            return;
        }
        System.out.println("Deserialized Employee...");
        System.out.println("Name: " + e.name);
        System.out.println("Address: " + e.address);
        System.out.println("SSN: " + e.SSN);
        System.out.println("Number: " + e.number);
    }
}

 

上一篇:三级联动


下一篇:Spring-mvc-Restful风格实现增删改查