null其实是二进制0, 你直接写入二进制0到文件末尾就行。读出的时候简单判断读到的字符是不是二进制0,应该就可以了。
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(null);
out.writeObject(1);
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
System.out.println(in.readObject());
System.out.println(in.readObject());
} catch (Exception e) {
e.printStackTrace();
}