import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class FileUtil { public FileUtil() { } public void save(String fileName, Object o) throws Exception { String path = SharedData.getAppDir(); File dir = new File(path); dir.mkdirs(); File f = new File(dir, fileName); if (f.exists()) { f.delete(); } FileOutputStream os = new FileOutputStream(f); ObjectOutputStream objectOutputStream = new ObjectOutputStream(os); objectOutputStream.writeObject(o); objectOutputStream.close(); os.close(); } public Object readObject(String fileName) throws Exception { String path = SharedData.getAppDir(); File dir = new File(path); dir.mkdirs(); File file = new File(dir, fileName); InputStream is = new FileInputStream(file); ObjectInputStream objectInputStream = new ObjectInputStream(is); Object o = objectInputStream.readObject(); return o; } }
存数据方法
public static void saveBaseData() { FileUtil fu = new FileUtil(); try{ fu.save("taocandishzuofa", tcdishzuofa);
}catch(Exception e) { e.printStackTrace(); } }
取数据方法
public static void readBaseData() { FileUtil fu = new FileUtil(); try{
tcdishzuofa = (ArrayList<TaoCanDishZuoFa>) fu.readObject("taocandishzuofa"); }catch(Exception e) { e.printStackTrace(); } }
最后在需要的地方调用就行了、