writeObject与readObject的使用例子

写了一段程序,这段程序是用来:
传入1个用户id以及数据data,
然后在网站的cache目录下生成${employee_id}.ser的文件,把data写进去。
如果该文件存在,则删掉再重新创建下。

    public static ArrayList> getQueryData(String employee_id) {
String dataFile = webPath + "cache/" + employee_id + ".ser";
System.out.println(dataFile);
ArrayList> result = new ArrayList>();
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(dataFile));
result = (ArrayList>) in.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();
}
return result;
}

public static void writeQueryData(String employee_id, ArrayList> data) {
String dataFile = webPath + "cache/" + employee_id + ".ser";
try {
File f = new File(dataFile);
System.out.println(f.getAbsolutePath());
if (f.exists()) {
f.delete();
f.createNewFile();
}
System.out.println(f.getAbsolutePath());
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(data);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

你可能感兴趣的:(writeObject与readObject的使用例子)