writeObject与readObject的应用例子
writeObject与readObject的使用例子
写了一段程序,这段程序是用来:
传入1个用户id以及数据data,
然后在网站的cache目录下生成${employee_id}.ser的文件,把data写进去。
如果该文件存在,则删掉再重新创建下。
写了一段程序,这段程序是用来:
传入1个用户id以及数据data,
然后在网站的cache目录下生成${employee_id}.ser的文件,把data写进去。
如果该文件存在,则删掉再重新创建下。
public static ArrayList<HashMap<String, String>> getQueryData(String employee_id) { String dataFile = webPath + "cache/" + employee_id + ".ser"; System.out.println(dataFile); ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String,String>>(); try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(dataFile)); result = (ArrayList<HashMap<String, String>>) 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<HashMap<String, String>> 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(); } }