node操作 windows的appdata本地缓存文件
const os = require('os');
const path = require("path");
const fs = require("fs");
var homedir = os.homedir();
function mkdirs(dirpath) {
if (!fs.existsSync(path.dirname(dirpath))) {
mkdirs(path.dirname(dirpath));
}
fs.mkdirSync(dirpath);
}
function createDir(myPath){
fs.existsSync(myPath) == false && mkdirs(myPath);
}
var _x = Symbol("x");
class AppData{
constructor(dbnm){
if(!dbnm){
throw new Error("the database name is needless");
return;
}
dbnm = dbnm + ".info";
this.apppath = path.join(homedir,"/AppData/Local/excelMaster/", dbnm);
createDir(path.dirname(this.apppath));
}
connect(cd){
cd = cd || function(){};
fs.readFile(this.apppath,"utf-8",function(err,res){
if(err){
this[_x] = {};
}else{
var str = Buffer.from(res, 'base64').toString("utf8");
if(str){
try{
this[_x] = JSON.parse(str);
}catch(e){
this[_x] = {};
}
}else{
this[_x] = {};
}
}
cd(err,res);
});
}
connectSync(){
try{
var res = fs.readFileSync(this.apppath,"utf-8");
var str = Buffer.from(res, 'base64').toString("utf8");
if(str){
try{
this[_x] = JSON.parse(str);
}catch(e){
this[_x] = {};
}
}else{
this[_x] = {};
}
}catch(e){
this[_x] = {};
}
}
get(k){
return this[_x][k];
}
set(k,val,callback){
callback = callback || function(){};
this[_x][k] = val;
const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
fs.writeFile(this.apppath,buf.toString('base64'),callback);
}
setSync(k,val){
this[_x][k] = val;
const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
fs.writeFileSync(this.apppath,buf.toString('base64'));
}
setSyncObj(obj){
for(var i in obj){
this[_x][i] = obj[i];
}
const buf = Buffer.from(JSON.stringify(this[_x]), 'utf8');
fs.writeFileSync(this.apppath,buf.toString('base64'));
}
has(k){
return k in this[_x];
}
keys(){
return Object.keys(this[_x]);
}
values(){
return Object.values(this[_x]);
}
drop(){
this[_x] = {};
const buf = Buffer.from(JSON.stringify({}), 'utf8');
fs.writeFileSync(this.apppath,buf.toString('base64'));
}
disconnect(){
this.apppath = null;
delete this[_x];
}
}
module.exports = AppData;
使用方式
var AppData = require("./appdata");
var p = new AppData("abc");
p.connectSync();
//p.setSync("manny","28");
console.log(p.get("manny"))
p.disconnect();
node操作 windows的appdata本地缓存文件的更多相关文章
- Windows下搭建本地SVN服务器【转】
转自:http://www.linuxidc.com/Linux/2015-01/111563.htm 本文介绍Windows下搭建本地SVN服务器的方法,网上资料比较少也比较旧,大都介绍的是旧版本S ...
- 强势解决:windows 不能在本地计算机中起动Tomcat参考特定错误代码1
Tomcat添加系统服务:service.bat install 启动本服务的时候却提示“windows 不能在本地计算机中起动 Apache Tomcat参考特定错误代码1,若不是Microsoft ...
- Windows不能在本地计算机启动OracleDBConsoleorcl .错误代码2
Windows 不能在 本地计算机 启动 OracleDBConsoleorcl.有关更多信息,查阅系统事件日志.如果这是非 Microsoft 服务,请与服务厂商联系,并参考特定服务错误代码 2. ...
- Windows 不能在 本地计算机 启动 SQL Server 服务 错误代码126
本文转自:http://www.cnblogs.com/yuerdongni/archive/2012/08/18/2645140.html 在使用SQL2005(或2008)是可能会遇到错误提示: ...
- windows不能在本地计算机启动SQL Server(MSSQLSERVER)
windows不能在本地计算机启动sql server 在登录数据库的时候,发现数据库不能登录,提示[无法连接到实例],很明显这是因为数据库服务没有启动导致的,我们打开[服务]启动相应的SQL数据库服 ...
- 【转】解决Windows不能在本地计算机启动apache tomcat
http://blog.163.com/ftskwsg@126/blog/static/5623853020094494117827/ 这个方法解决了我的问题. 在windows下以服务的方式启动时提 ...
- winreg操作windows注册表详解示例
#coding:utf-8 #=====================================================================#=====本程序演示了WINR ...
- C语言操作WINDOWS系统存储区数字证书相关函数详解及实例
C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...
- C# 操作windows服务[启动、停止、卸载、安装]
主要宗旨:不已命令形式操作windows服务 static void Main(string[] args) { var path = @"E:\开发辅助项目\WCF\WCF.Test\WC ...
随机推荐
- ADT下载地址(申明:来源于网络)
ADT下载地址(含各版本),最新ADT-23.0.6(申明:来源于网络) 地址:http://write.blog.csdn.net/mdeditor
- npm构建保存 code ELIFECYCLE解决办法
参考文档https://blog.csdn.net/gh254172840/article/details/78871573 使用npm构建报错 解决办法,进入工作目录 rm -rf node_mod ...
- ajax传输中文参数乱码,本地使用tomcat不乱码,liunx+weblogic乱码
公司项目有个问题,ajax请求含中文,无论是post方式还是get方式.本地使用tomcat不乱码,liunx+weblogic都乱码.并且用以往encodeURIComponent()并在后台解码之 ...
- 温故KMP算法
最近由于某些原因,又回顾了一次KMP算法.上一次回顾KMP算法还是在刷题的时候遇到的: http://blog.csdn.net/dacc123/article/details/50994611 在我 ...
- GIAC深圳站 | 2018年不可错过的全球互联网架构大会!
2018年6月1~2日,GIAC 全球互联网架构大会将于深圳华侨城洲际酒店举行!GIAC全球互联网架构大会是由msup和高可用架构技术社区联合举办的面向架构师.技术负责人及高端技术从业人员的技术架构大 ...
- [No0000F8]override和new的区别
override 1. override是派生类用来重写(或覆盖)基类中方法的: 2. override不能重写非虚方法和静态方法: 3. override只能重写用virtual.abstract. ...
- Vue.js最佳实践
Vue.js最佳实践 第一招:化繁为简的Watchers 场景还原: created(){ this.fetchPostList() }, watch: { searchInputValue(){ t ...
- Lombok安装及使用
为什么要使用Lombok: 虽然一般的IDE(eclipse,Intellij)都有自动生成代码的功能,如:生成setter,getter,toString,equels,hashcode等.但是如果 ...
- i-chips融合芯片分析
适合做图像变形和融合 http://www.i-chips.co.jp/products/ma_non-deployment/c786.html 下面视频其实用了2个芯片,其中IP00C733用原始信 ...
- Python判断字符串是否为字母或者数字
严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会Falseisalnum()必须是数字和字母的混合isalpha()不区分大小写 str_1 = "123" str ...