ObjectOutputStream(对象的输出流类) : 该类主要是用于把对象数据写出到文件上的。
ObjectInputStream(对象的输入流类 ) : 把硬盘中的对象数据读取回来。
对象的输入输出流要注意的实现:
- 使用ObjectOutputStream的writeObject方法时候,只能写出实现了Serializable接口的对象。Serializable 接口没有任何的方法,这种接口我们称作为标识接口。
- 对象反序列化的时候创建对象是不会调用构造方法的。
- 我们把对象写到文件上的时候,文件除了记录对象的一些信息以外,还记录了class的版本号(serialVersionUID), 这个版本号是通过一个类的类名、 包名、 工程名、成员一起算出的一个id号。
- 在反序列化的时候,jvm会使用本地class文件算出一个id号与文件记录的id号进行对比,如果不一致,反序列化失败。
- 如果一个类的成员可能在后期会发生改动,那么可以在序列化之前就指定一个serialVersionUID , 如果一个类一家指定了一个serialVersionUID那么java虚拟机则不会再计算该class文件的serialVersionUID了。
- 如果一个类的某些成员不想被序列化到硬盘上,可以使用关键字transient修饰。 static修改的成员也不会被序列化到
- 如果一个类的内部维护了另外一个类对象,那么另外一个类也必须 要实现Serializable接口。
//地址类
class Address implements Serializable{
String country;
String city;
public Address(String country, String city) {
super();
this.country = country;
this.city = city;
}
}
//用户类
class User implements Serializable{
private static final long serialVersionUID = 1L;
Address address = new Address("中国","广州");
String userName;
int password;
transient int age; // transient 透明
public User(String userName, int password, int age) {
this.userName = userName;
this.password = password;
this.age =age;
}
@Override
public String toString() {
return "用户名:"+this.userName+" 密码:"+ this.password+" 年龄:"+ this.age;
}
}
public class Demo3 {
public static void main(String[] args) throws Exception {
writeObj();
// readObj();
}
//对象的反序列化-----> 读取硬盘中的对象到内存中。
public static void readObj() throws Exception{
//找到目标文件
File file = new File("f:\\obj.txt");
//建立数据的输入流对象
FileInputStream fileInputStream = new FileInputStream(file);
//建立对象的输入流
ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
//读取对象的数据
User user = (User) inputStream.readObject(); //反序列化的时候需要创建对象, 创建对象需要依赖什么? Class文件
System.out.println("对象的信息:"+ user);
}
//把对象写到文件上------>对象的序列化。
public static void writeObj() throws IOException{
User user = new User("admin",123,18);
//找到目标
File file = new File("f:\\obj.txt");
//建立数据的输出通道
FileOutputStream fileOutputStream = new FileOutputStream(file);
//建立对象的输出流对象
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
//把对象写出到输出流中
objectOutputStream.writeObject(user);
//关闭资源
objectOutputStream.close();
}
}
练习:
使用ObjectInputStream/OutputStream类重新实现登录注册功能
import java.io.*;
import java.util.HashSet;
import java.util.Scanner;
class User implements Serializable{
private final long serialVersionUID = 1L;
String name;
String passwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public User(String name,String passwd){
this.name = name;
this.passwd = passwd;
}
@Override
public int hashCode() {
return this.name.hashCode();
}
@Override
public boolean equals(Object obj) {
User user = (User)obj;
return (user.name.equals(this.name));
}
}
public class Demo01 {
static HashSet<User> set = new HashSet <User>();
static File userFile = new File("C:/Users/Administrator/Desktop/register");
public static void main(String[] args) throws IOException, ClassNotFoundException {
readFromFile();
while(true){
System.out.println("请选择功能");
System.out.println("1.登录 2.注册 3.退出");
int sel = new Scanner(System.in).nextInt();
switch(sel){
case 1:
login();
break;
case 2:
register();
break;
case 3:
exit();
break;
default:
System.out.println("没有改该选项,请重新选择");
break;
}
}
}
/**
* 登录*/
public static void login(){
System.out.println("用户名:");
String name = new Scanner(System.in).nextLine();
System.out.println("密码:");
String passwd = new Scanner(System.in).nextLine();
for(User user :set){
if(user.getName().equals(name) ){
if(user.getPasswd().equals(passwd)){
System.out.println("登录成功");
return;
}
else{
System.out.println("密码错误");
return;
}
}
}
System.out.println("用户不存在");
}
/**
* 注册*/
public static void register (){
while(true){
System.out.println("请输入以下信息:");
System.out.println("用户名:");
String name = new Scanner(System.in).nextLine();
System.out.println("密码:");
String passwd = new Scanner(System.in).nextLine();
if(set.add(new User(name,passwd))){
System.out.println("注册成功");
break;
}else{
System.out.println("用户名已存在");
}
}
}
/**
* 从文件中读取数据
* @throws ClassNotFoundException */
public static void readFromFile() throws IOException, ClassNotFoundException{
if(!userFile.exists()){
userFile.createNewFile();
}else{
FileInputStream FileIStream= new FileInputStream(userFile);
ObjectInputStream objIStream = new ObjectInputStream(FileIStream);
set = (HashSet<User>) objIStream.readObject();
System.out.println(set.size());
objIStream.close();
}
}
/**
* 退出程序*/
public static void exit() throws IOException {
if(!userFile.exists()){
userFile.createNewFile();
}
FileOutputStream outStream = new FileOutputStream(userFile);
ObjectOutputStream objOutStream = new ObjectOutputStream(outStream);
objOutStream.writeObject(set);
objOutStream.close();
System.out.println("已退出");
System.exit(0);
}
}