零基础学Java第二十一天之IO流之对象流

IO流之对象流

1、对象流

1、理解

将对象写入到文件,将文件里的对象读取到程序中

class ObjectInputStream – 对象输入流

class ObjectOutputStream – 对象输出流

序列化/钝化:程序里的对象 写入到 文件中

反序列化/活化:文件中的对象 读取到 程序中

2、注意

  1. 对象所属的类必须实现序列化接口 - Serializable
  2. Serializable接口没有任何抽象方法
  3. 没有任何抽象方法的接口叫做标记型接口
  4. Serializable的实现类必须添加序列化ID - serialVersionUID
  5. transient修饰的成员属性,该属性不会随着对象写入到文件中
  6. 静态属性不会随着对象写入到文件中,因为静态属性不属于对象

3、使用

1、利用 对象输出流 向文件写入数据
package com.xx.IO04;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

public class Test01 {
	public static void main(String[] args) throws IOException {
		//1.创建流对象
		ObjectOutputStream ois = new ObjectOutputStream(new FileOutputStream("xx.txt"));
		//2.写入数据
		ois.write(100);//写入int类型的数据
		ois.writeDouble(123.123);//写入double类型的数据
		ois.writeUTF("yxased");//写入UTF-8编码格式的字符串类型的数据
		ois.writeObject(new Date());//写入对象类型的数据
		//3.关闭资源
		ois.close();
	}
}
2、利用 对象输入流 读取文件里的数据
package com.xx.IO04;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;


public class Test02 {
	/**
	 * 知识点:利用 对象输入流 读取文件里的数据
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		//1.创建对象流对象
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("xx.txt"));
	
		//2.读取数据
		int read = ois.read();
		System.out.println(read);
		double d = ois.readDouble();
		String utf = ois.readUTF();
		Date Date = (Date)ois.readObject();
		System.out.println(d);
		System.out.println(utf);
		System.out.println(Date);
        //关闭资源
		ois.close();
	}
}

3、自定义对象
package com.xx.io04;

import java.io.Serializable;

public class User implements Serializable{

	private static final long serialVersionUID = 8798250558176997713L;
	
	private String username;
	private transient String password;
	private String nickName;
	private String role;
	private double hp;
	private double mp;
	
	public User() {
	}

	public User(String username, String password, String nickName, String role, double hp, double mp) {
		this.username = username;
		this.password = password;
		this.nickName = nickName;
		this.role = role;
		this.hp = hp;
		this.mp = mp;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public double getHp() {
		return hp;
	}

	public void setHp(double hp) {
		this.hp = hp;
	}

	public double getMp() {
		return mp;
	}

	public void setMp(double mp) {
		this.mp = mp;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + ", nickName=" + nickName + ", role=" + role
				+ ", hp=" + hp + ", mp=" + mp + "]";
	}
}

4、利用 对象输出流 向文件写入自定义对象
package com.xx.IO04;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class Test03 {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		
		//创建溜对象
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("xx.txt"));
		//写入数据
		oos.writeObject(new User("1123423479", "123123", "撕裂的忧伤", "艾瑞莉娅", 1200, 800));
		oos.writeObject(new User("2323543430", "321321", "沙漠野猪", "DJ", 1000, 1000));
		oos.writeObject(new User("1234532143", "123456", "玛卡巴卡", "元歌", 800, 1200));
		
		//关闭资源
		oos.close();
	}
}

5、利用 对象输入流 读取文件里的自定义对象
package com.xx.IO04;

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

public class Test04 {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		//1.创建流对象
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("xx.txt"));
	
		//2.读取数据
		User user;
		try {
			while((user=(User)ois.readObject()) != null){
				System.out.println(user);
			}
		} catch (EOFException e) {
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值