日期:2014-05-20 浏览次数:21227 次
/////////////////////////////////////////////////////////////////////////////////////////////////////////
private static <T> void writeObjectToFile(T t, String fileName) {// 将一个对象 写入文件,对象是可序列化的
if (t == null)
return;// 对象不存在 默认不写
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(t);
oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);
} catch (Exception e) {
} finally {
try {
if (oos != null)
oos.close();
} catch (Exception e) {
}
}
}
private static <T> T readObjectFromFile(String fileName) {// 从文件中读出一个对象
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(fileName));
T t = (T) ois.readObject();
return t;
} catch (Exception e) {
} finally {
try {
if (ois != null)
ois.close();
} catch (Exception e) {
}
}
return null;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
------解决方案--------------------
传递对象然后使用就可以了 上面两楼已经给出答案了