对象流的写入
public static void objectWriteStream(File file, Message message ){
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream(file,false));
objectOutputStream.writeObject(message);
objectOutputStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(objectOutputStream != null){
try {
objectOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
对象流的读取
public static Message objectReadStream(File file){
ObjectInputStream objectInputStream = null;
Message message = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(file));
//强转成Object类型。
message = (Message) objectInputStream.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(objectInputStream != null){
try {
objectInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return (message);
}