第十四章 io(1)

2015、6、30

1、储存状态的两种选择

(1)如果只有自己写的java程序会用到这些数据:

        用序列化(serialization),将被序列化的对象写到文件中。然后可以让你的程序去文件中读取序列化的对象并将他们展开成序列化之前的状态。

(2)如果数据需要被其他的程序引用:

        写一个纯文本文件。用其他程序可以解析的特殊字符写到文件中。

2、当对象被序列化时,该对象引用的实例变量也会被序列化。且所有被引用的对象也会被序列化。

3、Serialization接口又被称为marker或tag类的标记用接口,这个接口没有任何方法需要实现。他的作用是声明该类可以被序列化。

      如果一个类没有标记Serialization接口,则对他进行序列化,会报错。

范例:(这里的例子保存的都是人类无法识别的编码,用此方法可以保存中文)

package head;
import java.io.*;
public class Box implements Serializable{
	
	private int width;
	private int height;
	
	public void setWidth(int w){
		width=w;
	}
	public void setHeight(int h){
		height=h;
	}
	
	public static void main(String[] args){
		Box myBox=new Box();
		myBox.setWidth(50);
		myBox.setHeight(20);
		
		try{
			FileOutputStream fs=new FileOutputStream("foo.ser");
			ObjectOutputStream os=new ObjectOutputStream(fs);
			os.writeObject(myBox);
			os.close();
		}catch(Exception e){e.printStackTrace();}
	}
}

4、如果需要序列化得程序中需要跳过某个实例变量,则将这个实例变量标记为transient(瞬时)

transient String a;

 

 

 

5、解序列化:将对象序列化的重点是你可以在事后,在不同的java虚拟机执行期(甚至不是同一个java虚拟机),把对象恢复到储存时的状态。

*如果对象在继承树上有某个不可序列化的祖先,则会执行这个祖先的构造函数(也就是新建一个祖先的对象)。对象的实例化变量会被还原成序列化时的值。

transient变量会赋值为null或数据的默认值。

*static代表的静态变量,代表每个类共享,所以对象被还原时,静态变量会维持在类里原本的样子,而不是储存时的样子。

package head;
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{
			FileOutputStream fs=new FileOutputStream("game.ser");
			ObjectOutputStream os=new ObjectOutputStream(fs);
			os.writeObject(one);
			os.writeObject(two);
			os.writeObject(three);
			os.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		one=null;
		two=null;
		three=null;
		
		//读取
		try{
			FileInputStream fi=new FileInputStream("game.ser");
			ObjectInputStream is=new ObjectInputStream(fi);
			GameCharacter oneRestore=(GameCharacter)is.readObject();
			GameCharacter twoRestore=(GameCharacter)is.readObject();
			GameCharacter threeRestore=(GameCharacter)is.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());
			is.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}


 

package head;
import java.io.*;
public class GameCharacter implements Serializable{
	private int power;
	private String type;
	private String[] weapons;
	
	public GameCharacter(int p,String t,String[] w){
		power=p;
		type=t;
		weapons=w;
	}
	
	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;
	}
}




 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值