IO流之对象流
1、对象流
1、理解
将对象写入到文件,将文件里的对象读取到程序中
class ObjectInputStream – 对象输入流
class ObjectOutputStream – 对象输出流
序列化/钝化:程序里的对象 写入到 文件中
反序列化/活化:文件中的对象 读取到 程序中
2、注意
- 对象所属的类必须实现序列化接口 - Serializable
- Serializable接口没有任何抽象方法
- 没有任何抽象方法的接口叫做标记型接口
- Serializable的实现类必须添加序列化ID - serialVersionUID
- transient修饰的成员属性,该属性不会随着对象写入到文件中
- 静态属性不会随着对象写入到文件中,因为静态属性不属于对象
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;}@Overridepublic 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 blocke.printStackTrace();}}
}