自我练习VI

自我练习

package com.cs.demol;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.text.FieldPosition;

public class Test1 {
	public static void main(String[] args) {
		try {

			FileOutputStream outputStream = new FileOutputStream("D:\\demo.txt");

			ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
			Hero hero = new Hero();

			hero.setName("剑圣");
			hero.setGongjili("2300");
			objectOutputStream.writeObject(hero);
			
			System.out.println(hero);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();

		}
	}

}

在这里插入图片描述

package com.cs.demol;

import java.io.Serializable;

public class Hero implements Serializable{
	private String name;
	private String gongjili;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGongjili() {
		return gongjili;
	}
	public void setGongjili(String gongjili) {
		this.gongjili = gongjili;
	}
	@Override
	public String toString() {
		return "Hero [gongjili=" + gongjili + ", name=" + name + "]";
	}

}

package com.cs.demol;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.text.FieldPosition;

public class Test2 {
	public static void main(String[] args) {
		try {

			FileInputStream fileInputStream = new FileInputStream("D:\\demo.txt");

			ObjectInputStream objectInputStream= new ObjectInputStream(fileInputStream);
			Hero hero = (Hero) objectInputStream.readObject();

			
			
			
			System.out.println("名称:"+hero.getName()+"攻击力:"+hero.getGongjili()+"");
			objectInputStream.close();
			fileInputStream.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();

		}
	}

}

在这里插入图片描述

发布了72 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/BOGEWING/article/details/102728571