head first java ( 14章 )

- 对象的状态和行为

  对象有状态和行为两种属性, 行为存在于类中, 状态存在于个别对象中( 想内存 )

- 保存状态的两种方法 

  比如你编写了一个游戏程序, 里边有精灵, 矮人, 魔法师, 你肯定需要保存他们的状态, 比如 生命值, 武力值 等等, 那么如何保存呢?

  imageimage

- 创建序列化对象写入文件步骤

  image

  image

  image

- 在堆上的对象 –> 被实例化的对象

  image

  如果存储的实例变量不仅仅是标量类型, 如果是引用类型怎么办? 存储的目的在于恢复, 如果恢复实例变量是引用类型呢 ?

  image 

  当对象被序列化时, 被该对象引用的实例变量也会被序列化,且所有被引用的对象也会被序列化,最棒的是,这些操作都是自动的

  image

- 如果要让类能被序列化, 就必须实现 Serializable 接口

  例如 public class Box implements Serializable { // 这个接口没有方法需要实现, 只是说明这个类可以被序列化

  objectOutputStream.writeobject(myBox); // 这个myBox 类必须实现 Serializable 接口

  如果某实例变量不能或不应该被序列化, 就把它标记为 transient(瞬时)的

  transient String currentID;

- 解序列化, 还原对象

  image

  image

  image

  image

  image

  image

  image

- 存储与恢复游戏人物 源码及分析 

GameTest/*
 * File: GameSaveTest.java
 * ------------------------------
 * 
 */

import java.io.*;


public class GameSaveTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GameCharacter one = new GameCharacter(50, "Elf", new String[] {"bow", "swod", "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 (IOException ex) {
			ex.printStackTrace();
		}
		one = null;
		two = null;
		three = null;
		
		try {
			ObjectInputStream is = new ObjectInputStream(new FileInputStream("Game.ser"));
			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());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		
	}

}

 

GameCharacter/*
 * File: GameCharacter.java
 * ----------------------------
 * 
 */

import java.io.*;

public class GameCharacter implements Serializable {
	
	
	/* 
	 * structure function 
	 * augument p: power
	 * arugment t: type
	 * argument w: 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;
	}
		
/* private instance values */
	private int power;
	String type;
	String[] weapons;

	
}

 

 

- 将字符串写入文本文档

  如果你想要非 java 程序来读取对象, 那么就可以存储成文本文档 ( 不过这样有点不安全 )

  image

  使用 Filewriter

  FileWriter writer = new FileWriter(“Foo.txt”);

  writer.write(“hello foo!”);  // 以字符串做参考

  writer.close();

  注意: 以上代码要有 try catch .

- java.io.File 这个类 

  file这个类代表磁盘文件, 而不是文件中的内容.

  File 有个很有用的功能就是它提供一种比使用字符串文件名来表示文件更安全的方式.( 可以检查路径是否合法 )

  image

- 缓冲区的奥妙

  image

  image

- 读取文件 源代码分析 

  image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值