<html>
<head>
<title>
IniConfig
</title>
<script language=javascript>
String.prototype.trim = function()
{
return this.replace(/(^\s+)|(\s+$)/g,'');
};
IniConfig=function(iniFileName)
{
this.iniFileName = iniFileName;
this._iniSecDictionary = new Array();
this.fso = new ActiveXObject("Scripting.FileSystemObject");
};
IniConfig.prototype._checkFile = function()
{
if(!this.fso.FileExists(this.iniFileName))
{
this.fso.CreateTextFile(this.iniFileName,true,true);
}
};
IniConfig.prototype.load = function()
{
this._checkFile();
var currSecName = null;
var fs = this.fso.OpenTextFile(this.iniFileName,1,false,-1);
while(!fs.AtEndOfStream)
{
var strLine = fs.ReadLine().trim();
if(strLine.length > 0)
{
var firchCh = strLine.substr(0,1);
if(firchCh != ';')
{
if(firchCh == '[')
{
var secName = strLine.substr(1,strLine.length - 2);
currSecName = secName;
this._iniSecDictionary[secName] = new Array();
}
else
{
var idx = strLine.indexOf('+');
var strKey = strLine.substring(0,idx);
var strVal = strLine.substr(idx + 1);
if(currSecName == null)
{
throw("Ini文件格式不正确!");
}
this._iniSecDictionary[currSecName][strKey] = strVal;
}
}
}
}
fs.Close();
fs = null;
};
IniConfig.prototype.save = function()
{
this._checkFile();
var dic = this._iniSecDictionary;
var currSecName = null;
var fs = this.fso.OpenTextFile(this.iniFileName,2,true,-1);
for(var sec in dic)
{
fs.WriteLine('[' + sec + ']');
alert('[' + sec + ']');
for(var key in dic[sec])
{
fs.WriteLine(key + '+' + dic[sec][key]);
alert(key + '+' + dic[sec][key]);
}
}
fs.Close();
fs = null;
};
IniConfig.prototype.get = function(secName,keyName)
{
var dic = this._iniSecDictionary;
try
{
return dic[secName][keyName];
}
catch(e)
{
return '';
}
};
IniConfig.prototype.set = function(secName,keyName,val)
{
var dic = this._iniSecDictionary;
try
{
if(dic[secName] == null)
{
dic[secName] = new Array();
}
dic[secName][keyName] = val;
}
catch(e)
{
alert(e.message);
}
};
function $(objID){
return document.getElementById(objID);
}
</script>
<script language=javascript>
//初始化对象
var iniFileObj = null;
var iniFilePath = "";
iniFilePath = "C:\\Ini1.ini";
//加载文件,如果失败则创建文件
try
{
iniFileObj = new IniConfig(iniFilePath);
iniFileObj.load();
}
catch(e)
{
alert(e.message);
$("idBtnGetValues").enable = false;
$("idBtnSetValues").enable = false;
}
//获取值
function getValues(){
var sSeg = null;
var sKey = null;
var sValue = null;
sSeg = $("idSeg").value;
sKey = $("idKey").value;
sValue = iniFileObj.get(sSeg,sKey);
$("idValue").value = sValue;
alert("获取值:" + sValue);
}
//设置值
function setValues(){
var sSeg = null;
var sKey = null;
var sValue = null;
sSeg = $("idSeg").value;
sKey = $("idKey").value;
sValue = $("idValue").value;
iniFileObj.set(sSeg,sKey,sValue);
iniFileObj.save();
alert("设置值成功。");
}
</script>
</head>
<body>
Segment:<input id="idSeg" value=SEG1 /><br>
Keys:<input id="idKey" value=KEY1 /><br>
Values:<input id="idValue" value=Value1 /><br>
<input type=button id="idBtnGetValues" Value="getValues()" onClick="getValues();" />
<input type=button id="idBtnSetValues" Value="setValues()" onClick="setValues();" />
</body>
</html>
JavaScript实现文件以键值对的形式读写
3星 · 超过75%的资源 需积分: 34 111 浏览量
2011-01-24
16:51:02
上传
评论
收藏 1KB RAR 举报
minsheng0825
- 粉丝: 42
- 资源: 12
最新资源
- C语言基础-C语言编程基础之Leetcode编程题解之第30题串联所有单词的子串.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第29题两数相除.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第28题找出字符串中第一个匹配项的下标.zip
- 实验报告模板(1).docx
- C语言基础-C语言编程基础之Leetcode编程题解之第26题删除有序数组中的重复项.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第25题K个一组翻转链表.zip
- hnu计算机系统作业-计算机系统基础课程大作业.zip
- 树莓派app.apk
- C++的基于同态加密技术的匿名电子投票系统源码.zip
- SW建模格式图.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈