准备工作

准备user类

一个可序列化的类对象必须实现Serializable接口或者Externalizable接口

区别在于

  1. Serializable接口是一个标记接口,没有方法,不需要实现
  2. Externalizable接口是一个必须实现的接口

这里我们选择实现Serializable接口

并且添加serialVersionUID,提高版本的兼容性

准备一个user类,信息如下

1
2
3
4
5
6
class user implements Serializable {
private final static long serialVersionUID = 1L;
String name;
int age;
char sex;
}

准备user.txt文件

字符编码为UTF8

image-20220905160151436

ObjectOutputStream

可以将对象序列化到一个文件中

构造方法

public ObjectOutputStream(OutputStream out) throws IOException

可以传入OuterPutStream类对象和子类对象完成ObjectOuterputStream的构造

1
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("E:\\news.txt"));

write系列方法

image-20220905164854888

write方法及其重载参考:FileReader和FileWriter类方法详解

wirteObject:将指定的对象写入文件中

writeByte:写入一个8位字节

writeBoolean:写入一个布尔值

writeBytes:写一个字符串作为字节序列

writeChar:写一个16位的字符

wirteChars:写一个字符串作为一系列的字符

writeDouble:写一个64位的double到文件中

writeFields:将缓冲的字段写入流

writeFloat:写一个32位float到文件中

writeInt:将一个int型数据写入文件中

writeLong:将一个long型数据写入文件中

writeShort:将一个short型数据写入文件中

writeUTF:此字符串的原始数据写入格式为UTF-8

writeUnshared:将“非共享”对象写入文件中

1
2
3
4
5
6
7
8
9
ObjectOutputStream objectOutputStream = new ObjectOutputStream(Files.newOutputStream(Paths.get("E:\\user.txt")));
// 写入以下内容到文件中,这个过程为序列化过程
objectOutputStream.writeUTF("jack");
objectOutputStream.writeInt(18);
objectOutputStream.writeChar('男');
// 刷新缓冲区,将缓冲区的内容写入文件
objectOutputStream.flush();
// 关闭流
objectOutputStream.close();

结果

image-20220905172906434

flush、close

FileInputStream和FileOutputStream常用方法详解

reset

重置ObjectOutputStream流对象

public void reset() throws IOException

1
2
ObjectOutputStream objectOutputStream = new ObjectOutputStream(Files.newOutputStream(Paths.get("E:\\news.txt")));
objectOutputStream.reset();

useProtocolVersion

指定在编写流时使用的流协议版本

有两个版本

  1. PROTOCOL_VERSION_1
  2. PROTOCOL_VERSION_2

public void useProtocolVersion(int version) throws IOException

1
2
3
ObjectOutputStream objectOutputStream = new ObjectOutputStream(Files.newOutputStream(Paths.get("E:\\news.txt")));
// 指定为ObjectStreamConstants.PROTOCOL_VERSION_1版本
objectOutputStream.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_1);

ObjectInputStream

将文件中的对象反序列化到java程序中

构造方法

public ObjectInputStream(InputStream in) throws IOException

可以传入InputStream对象或其子类

1
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("E:\\user.txt"));

read系列方法

image-20220906072841589

read方法及其重载:见FileReader和FileWriter类方法详解

readFields:从流中读取持久性字段,并通过名称获取它们

readBoolean:读取布尔值

readByte:读取8位字节

readChar:读取16位字符

readDouble:读取64位浮点数

readFloat:读取32位浮点数

readFully

  1. void readFully(byte b[]) throws IOException

    读取整个字节数组b

  2. void readFully(byte b[], int off, int len) throws IOException

    读取字节数组的[off, off + len - 1]部分

readInt:读取一个32位int

readShort:读取16位short

readUnsignedByte:读取一个无符号的8位字节

readUnsignedShort:读取一个无符号的16位short

readUTF:以UTF-8的形式读取字符串

readObject:读取一个对象

readUnshared:读取一个非共享的对象

readLine:已过时,一次读取一行内容,慎用

defaultReadObject:从此流读取当前类的非静态和可序列化字段

1
2
3
4
5
6
7
8
9
10
11
ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(Paths.get("E:\\user.txt")));
String name = objectInputStream.readUTF();
int age = objectInputStream.readInt();
char sex = objectInputStream.readChar();
System.out.println("name = " + name);
System.out.println("age = " + age);
System.out.println("sex = " + sex);
// 输出结果:
// name = jack
// age = 18
// sex = 男

skipBytes

使用此方法可以在文件读取过程中跳过读取指定字节数len

public int skipBytes(int len) throws IOException

1
2
ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(Paths.get("E:\\user.txt")));
objectInputStream.skipBytes(9);

skip、close、available

available:获取文件的总大小

skip:作用同skipBytes,具体细节不讨论

close:关闭流