14序列化和文件的输入、输出

1、让类能够被序列化,就实现Serializable接口(该接口没有任何方法需要实现,它的唯一目的就是声明有实现它的类是可以被序列化的

2、如果某类是可序列化的,则它的子类也自动地可以序列化

序列化:

import java.io.*;

public class Test implements Serializable{
    int width;
    int height;
    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeiget(int height) {
        this.height = height;
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.setWidth(50);
        t.setHeiget(100);

        try{
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("E:\info.ser"));
            os.writeObject(t);//个人理解:写入的是一个对象,能把对象转化成字节的形式只有实现序列化
            os.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }    
}

不提示

import java.io.*;

public class Test {
    public static void main(String[] args) {
        Test t = new Test();
        try {
            ObjectOutputStream fs = new ObjectOutputStream(new FileOutputStream("E:\Pond.ser"));
            fs.writeObject(t);//同样写入对象,编译通过,能正常执行,不提示需要实现Serializable
            fs.close();
        }catch(Exception e) {
            //
        }
    }
}

存储于恢复游戏人物:

人物信息:

import java.io.*;

public class GameCharacter implements Serializable {
    int power;
    String type;
    String[] weapons;

    public GameCharacter(int Power, String type, String[] weapons) {
        this.power = power;
        this.type = type;
        this.weapons = weapons;
    }

    public int getPower() {
        return power;
    }

    public String getType() {
        return type;
    }

    public String getWeapons() {
        String weaponList = "";
        for(int i = 0; i < weapons.length; i++) {
            weaponList += weapons[i] + " ";
        }
        return weaponList;
    }
}

存取&恢复

import java.io.*;

public class GameSaverTest {
    public static void main(String[] args) {
        GameCharacter one = new GameCharacter(50, "Elf", new String[] {"bow", "sword", "dust"});
        GameCharacter two = new GameCharacter(200, "troll", new String[] {"bare hands", "big ax"});
        GameCharacter three = new GameCharacter(120, "Magician", new String[] {"spells", "invisibility"});

        try {
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Game.ser"));
            os.writeObject(one);
            os.writeObject(two);
            os.writeObject(three);
            os.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
        one = null;
        two = null;
        three = null;

        try {
            ObjectInputStream ins = new ObjectInputStream(new FileInputStream("Game.ser"));
            GameCharacter oneRestore = (GameCharacter) ins.readObject();
            GameCharacter twoRestore = (GameCharacter) ins.readObject();
            GameCharacter threeRestore = (GameCharacter) ins.readObject();

            System.out.println("one's type" + oneRestore.getType());
            System.out.println("two's type" + twoRestore.getType());
            System.out.println("three's type" + threeRestore.getType());

        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}
原文地址:https://www.cnblogs.com/wmjlh/p/7325004.html