IO流
一、对象的序列化存储
ObjectInputStream与ObjectOutputStream:对象的序列化(持久化)存储。
void writeBoolean(boolean val)
写入一个 boolean 值。
void writeByte(int val)
写入一个 8 位字节。
void writeBytes(String str)
以字节序列形式写入一个 String。
void writeChar(int val)
写入一个 16 位的 char 值。
void writeChars(String str)
以 char 序列形式写入一个 String。
protected void writeClassDescriptor(ObjectStreamClass desc)
将指定的类描述符写入 ObjectOutputStream。
void writeDouble(double val)
写入一个 64 位的 double 值。
void writeFields()
将已缓冲的字段写入流中。
void writeFloat(float val)
写入一个 32 位的 float 值。
void writeInt(int val)
写入一个 32 位的 int 值。
void writeLong(long val)
写入一个 64 位的 long 值。
具体查阅Api文档。
write(int val);与writeInt(int val);的区别:前者写入的是4个8位的低8位。后者写的是4个8位。
writeObject();:写的是对象。
void writeObject(Object obj)
将指定的对象写入 ObjectOutputStream。
例:写入对象
import java.io.*;
class Person
{
String name;
int age;
static String country = "cn";
Person(String name,int age,String country){
this.name = name;
this.age = age;
this.country = country;
}
public String toString(){
return name+":"+age+":"+country;
}
}
class Demo
{
public static void main(String []args) throws Exception{
writeObj(new Person("lisi",20,"en"));
}
public static void writeObj(Person p) throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Info.txt"));
oos.writeObject(p);
oos.close();
}
}
分析:运行时报了异常,查看Api。NotSerializableException没有序列化异常。
当实例需要具有序列化接口时,抛出此异常。序列化运行时或实例的类会抛出此异常。参数应该为类的名称。也就是说要实现Serializable接口。查阅Api,发现Serializable没有方法。这种接口称为标记接口。
修改:
import java.io.*;
class Person implements Serializable
{
String name;
int age;
static String country = "cn";
Person(String name,int age,String country){
this.name = name;
this.age = age;
this.country = country;
}
public String toString(){
return name+":"+age+":"+country;
}
}
class Demo
{
public static void main(String []args) throws Exception{
writeObj(new Person("lisi",20,"en"));
}
public static void writeObj(Person p) throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Info.txt"));
oos.writeObject(p);
oos.close();
}
}
写入成功,但写入的其实是二进制文档,记事本解码会出现乱码。
例:读取对象
import java.io.*;
class Person implements Serializable
{
String name;
int age;
static String country = "cn";
Person(String name,int age,String country){
this.name = name;
this.age = age;
this.country = country;
}
public String toString(){
return name+":"+age+":"+country;
}
}
class Demo
{
public static void main(String []args) throws Exception{
//writeObj(new Person("lisi",20,"en"));
readObj();
}
public static void writeObj(Person p) throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Info.txt"));
oos.writeObject(p);
oos.close();
}
public static void readObj() throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Info.txt"));
Person p = (Person)ois.readObject();
System.out.println(p.toString());
}
}
读取成功,但是发现country值没有修改成功。country被static修饰存在方法区内,而ObjectOutputStream写入的是堆内存中的数据。两个区域处不同位置,所以static修饰的变量不能被序列化。若要使非静态成员也不序列化,可以在变量的类型前加transizent。
例:
import java.io.*;
class Person implements Serializable
{
String name;
transient int age;
static String country = "cn";
Person(String name,int age,String country){
this.name = name;
this.age = age;
this.country = country;
}
public String toString(){
return name+":"+age+":"+country;
}
}
特殊之处:
import java.io.*;
class Person implements Ser