java 序列化保存到磁盘的相关问题


package com.taskManager.connectionStation;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.taskManager.connectionStation.taskstation.TaskModel;

/**
* 对对象的保存操作
*
* @author Sammor
* @date 2011-1-9
*/
public class TaskSaveOrReadUtil<T> {

private static Log log = LogFactory.getLog(TaskSaveOrReadUtil.class);
private FileOutputStream fos;
private ObjectOutputStream oos;
private FileInputStream fis;
private ObjectInputStream ois;
private static String filePath = "d:/taskStroeMap.txt";

public TaskSaveOrReadUtil() {
init();
}

public TaskSaveOrReadUtil(String path) {
this.filePath = path;
init();
}

private void init() {
try {
fos = new FileOutputStream(filePath);
fis = new FileInputStream(filePath);
oos = new ObjectOutputStream(fos);
ois = new ObjectInputStream(fis);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

// 序列化对象到文件
public static void serialize() {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(filePath));

TaskModel tm = new TaskModel();
tm.setTargetErtuAddress("192.168.0.1");
tm.setApplyDataType(1);
tm.setTaskBeginTime(new Date());
tm.setTaskEndTime(new Date());
tm.setTaskType(1);
tm.getMapTest().put("hello", "world");
out.writeObject(tm); // 序列化一个会员对象
out.close();
} catch (Exception x) {
System.out.println(x.toString());
}

}

// 从文件反序列化到对象
public static void deserialize() {
try {
// 创建一个对象输入流,从文件读取对象
//ObjectInputStream in = new ObjectInputStream(new FileInputStream(
// filePath));
TaskModel user = (TaskModel) (in.readObject());
System.out.println(user.getMapTest().get("hello"));

ois.close();
} catch (Exception x) {
System.out.println(x.toString());
}

}

public void saveObject(T o) {

try {
//ObjectOutputStream out = new ObjectOutputStream(
// new FileOutputStream(filePath));

oos.writeObject(o);
} catch (IOException e) {
throw new RuntimeException("保存文件到磁盘出错");
} finally {
try {
oos.close();
} catch (IOException e) {
throw new RuntimeException("关闭流出错");
}
}

System.out.println("写入成功");
}

public T readObject() {

T t = null;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
filePath));
t = (T) in.readObject();
} catch (IOException e) {
e.printStackTrace();
log.warn("取不到本地信息文件");
return null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return t;
}

public static void main(String[] args) {
TaskSaveOrReadUtil ts = new TaskSaveOrReadUtil();

//先执行
// TaskModel tm = new TaskModel();
// tm.setApplyDataType(1);
// ts.saveObject(tm);

//执行完之后,注释掉,改执行下面代码
TaskModel tt = (TaskModel) ts.readObject();
System.out.println(tt.getApplyDataType());

}
}


不理解为什么静态的可以成功先读入,后程序重新取出,而普通的方法却不行。
出现错误:
[quote]java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.taskManager.connectionStation.TaskSaveOrReadUtil.readObject(TaskSaveOrReadUtil.java:116)
at com.taskManager.connectionStation.TaskSaveOrReadUtil.main(TaskSaveOrReadUtil.java:136)[/quote]

改为:

package com.taskManager.connectionStation;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.taskManager.connectionStation.taskstation.TaskModel;

/**
* 对对象的保存操作
*
* @author Sammor
* @date 2011-1-9
*/
public class TaskSaveOrReadUtil<T> {

private static Log log = LogFactory.getLog(TaskSaveOrReadUtil.class);
private FileOutputStream fos;
private ObjectOutputStream oos;
private FileInputStream fis;
private ObjectInputStream ois;
private static String filePath = "d:/taskStroeMap.txt";

public TaskSaveOrReadUtil() {
// init();
}

public TaskSaveOrReadUtil(String path) {
this.filePath = path;
//init();
}

/**
* 这里面不可以同时取到入流和出流
*/
private void init() {
try {
fos = new FileOutputStream(filePath);
fis = new FileInputStream(filePath);
oos = new ObjectOutputStream(fos);
ois = new ObjectInputStream(fis);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

// 序列化对象到文件
public static void serialize() {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(filePath));

TaskModel tm = new TaskModel();
tm.setTargetErtuAddress("192.168.0.1");
tm.setApplyDataType(1);
tm.setTaskBeginTime(new Date());
tm.setTaskEndTime(new Date());
tm.setTaskType(1);
tm.getMapTest().put("hello", "world");
out.writeObject(tm); // 序列化一个会员对象
out.close();
} catch (Exception x) {
System.out.println(x.toString());
}

}

// 从文件反序列化到对象
public static void deserialize() {
try {
// 创建一个对象输入流,从文件读取对象
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
filePath));
TaskModel user = (TaskModel) (in.readObject());
System.out.println(user.getMapTest().get("hello"));

in.close();
} catch (Exception x) {
System.out.println(x.toString());
}

}

public void saveObject(T o) {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(
new FileOutputStream(filePath));

out.writeObject(o);
} catch (IOException e) {
throw new RuntimeException("保存文件到磁盘出错");
} finally {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException("关闭流出错");
}
}

System.out.println("写入成功");
}

public T readObject() {

T t = null;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
filePath));
t = (T) in.readObject();
} catch (IOException e) {
e.printStackTrace();
log.warn("取不到本地信息文件");
return null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return t;
}

public static void main(String[] args) {
TaskSaveOrReadUtil ts = new TaskSaveOrReadUtil();

// TaskSaveOrReadUtil.serialize();
// TaskSaveOrReadUtil.deserialize();
// TaskModel tm = new TaskModel();
// tm.setApplyDataType(1);
// ts.saveObject(tm);
//
TaskModel tt = (TaskModel) ts.readObject();
System.out.println(tt.getApplyDataType());

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值