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

14 篇文章 1 订阅
6 篇文章 0 订阅

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

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

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

package cn.itsub.proxy.client.dao;

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

/**
 * 本地存储模块.
 *
 * @Author: 夏增明
 * @Date: 2021/10/09/13:41
 * @Description:
 */
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<String, Serializable> 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 extends Serializable> T getItem(String key) {
        synchronized (Storage.class) {
            return (T) data.get(key);
        }
    }

    /**
     * setItem时是否自动保存
     * @param autoSave
     */
    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<String, Serializable>) 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));
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程大玩家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值