gdntx 发表于 2013-2-4 20:19:54

利用Window temp存储数据

我们可以把数据存放在C:\Documents and Settings\Administrator\Local Settings\Temp里,要用的时候可以从temp取。
程序代码如下:
package com.javaeye.testjbpm;import java.io.File;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.io.Serializable;import java.util.HashMap;import java.util.Map;public class Persistence {private static Map variables = null;//java.io.tmpdir,不能改,这是JVM定义的private static String tmpfile = System.getProperty("java.io.tmpdir") + "/temp.object";//temp.object就是存放的文件名static{//加载文件try{if(new File(tmpfile).exists()){FileInputStream in = new FileInputStream(tmpfile);ObjectInputStream s = new ObjectInputStream(in);   variables = (Map)s.readObject();}if(variables == null){variables = new HashMap();}}catch(Exception e){e.printStackTrace();}}//设置一个变量的值public static void setVariable(String name,Serializable object){if(variables != null){variables.put(name, object);}try {FileOutputStream fos = new FileOutputStream(tmpfile);ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(variables);oos.flush();oos.close();fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();}}//获取一个变量的值public static Serializable getVariable(String name){if(variables != null){return (Serializable)variables.get(name);}return null;}} 
页: [1]
查看完整版本: 利用Window temp存储数据