栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java本地存储模块,IO流文件读写

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java本地存储模块,IO流文件读写

用IO流文件读写实现的本地存储模块,万条存储大概0.1秒。

存储文件位置在项目根目录,如果文件不存在会自动生成,

如果打成jar则存储文件会在同目录(在jar旁边)。

package cn.itsub.proxy.client.dao;

import java.io.*;
import java.util.HashMap;
import java.util.Map;


public class Storage {

    private static Storage instance;

    public static Storage getInstance() {
        if (instance == null) {
            synchronized (Storage.class) {
                if (instance == null) {
                    instance = new Storage();
                }
            }
        }
        return instance;
    }

    private Map data = new HashMap<>();
    private File dataFile;
    private boolean autoSave = true; //自动保存

    public Storage() {
        //URL path = this.getClass().getProtectionDomain().getCodeSource().getLocation();
        //File file = new File(path.getPath());
        //System.out.println(file);
        //File dir = file.getParentFile();
        dataFile = new File("storage.data");
        load();
    }

    public Storage setItem(String key, Serializable value) {
        synchronized (Storage.class) {
            data.put(key, value);
            if (autoSave) {
                save();
            }
        }
        return this;
    }

    public  T getItem(String key) {
        synchronized (Storage.class) {
            return (T) data.get(key);
        }
    }

    
    public Storage setAutoSave(boolean autoSave) {
        this.autoSave = autoSave;
        return this;
    }

    public boolean save() {
        synchronized (Storage.class) {
            try {
                OutputStream out = new FileOutputStream(dataFile);
                ObjectOutputStream oos = new ObjectOutputStream(out);
                oos.writeObject(data);
                oos.flush();
                oos.close();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean load() {
        synchronized (Storage.class) {
            try {
                System.out.println(dataFile.getAbsolutePath());
                if (!dataFile.exists()) {
                    dataFile.createNewFile();
                    save(); //新建的文件会导致ois发生eof异常,所以先用save初始化文件内容
                }
                InputStream in = new FileInputStream(dataFile);
                ObjectInputStream ois = new ObjectInputStream(in);
                data = (Map) ois.readObject();
                ois.close();
                System.out.println("加载完成");
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static void main(String[] args) {
        //基本使用
        Storage.getInstance().setItem("a",1);
        Storage.getInstance().setItem("b","Hello");
        int a = Storage.getInstance().getItem("a");
        String b = Storage.getInstance().getItem("b");
        System.out.println(a);
        System.out.println(b);
        //万条测试
        long t1 = System.currentTimeMillis();//开始时间
        Storage.getInstance().setAutoSave(false); //取消自动保存
        for (int i = 0; i < 10000; i++) {
            Storage.getInstance().setItem("FastProxy" + i, "HelloWorld" + i);
        }
        Storage.getInstance().save();//保存到文件
        Storage.getInstance().load();
        //计算时长
        double seconds = (System.currentTimeMillis()-t1)*0.001;
        System.out.println(String.format("总计耗时:%.2f", seconds));
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/331670.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号