• File的保存与读取


    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(); } }


    最后在需要的地方调用就行了、

  • 相关阅读:
    基于k8s搭建微服务日志收集中心
    分析java堆内存满时那些类占用内存居多
    yizimi 在 DMG 的板子库 (数据结构与算法)
    Contest 3/14
    基础算法训练1
    图论基础训练
    2021.03.09随笔
    树莓派 4B 安装 openEuler
    Docker 学习准备
    关于备案第二个服务器时遇到的问题
  • 原文地址:https://www.cnblogs.com/LIANQQ/p/3291555.html
Copyright © 2020-2023  润新知