对象流
2016-05-02 16:27
369 查看
对象流的写入
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);
}
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);
}
相关文章推荐
- Java基础知识
- java基础(一)
- 怎样学习java,漫谈java学习之路
- 设置jdk环境变量时lib中的rt.jar ,dt.jar ,tool.jar是什么,作用是什么
- Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例
- 学习日志---treemap
- java基础知识笔记
- java中web应用中重定向与转发的区别
- java基础篇---枚举详解
- java基础数据类型包装类
- equals与null
- 【Java基础】Java入门程序&基础数据类型(转换)&逻辑判断&运算等
- Java.基础(学习笔记)
- 一个简单的加密解密程序
- 20160411--相等测试与继承
- 【代码练习】用FIle.list()打印多级目录
- 【学习日记】有关java的IO的知识点总结
- JAVA基础-IO流
- 用java解决约瑟夫问题
- date and time