PhoneGap读写SD卡(键值对方式)
varfileSystem=null;
//等待加载PhonrGap
document.addEventListener("deviceready",onDeviceReady,true);
functiononDeviceReady(){
//requestthepersistentfilesystem
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,onFSSuccess,onError);
}
//加载phoneGap成功
functiononFSSuccess(fs){//alert("aaa="+fs);
fileSystem=fs;
}
functiononError(e){
alert(e);
//getById("#content").innerHTML="<h2>Error</h2>"+e.toString();
}
//areater缓存存储值防止同时调写文件多次,只有最后的写成功(javascript异步)
varareater=newArray();
varcount1=0;
//创建文件写文件
functionsave2File(fileName,key,value,keyValueSplit,recodeSplit){
//alert(6)
write2File(fileName,key,value,keyValueSplit,recodeSplit,"setSaveSuccess");
}
//创建文件写文件
functionwrite2File(fileName,key,value,keyValueSplit,recodeSplit,responseFun){//responseFun函数名显示结果
//alert(5)
varsaveSuccess=true;
varpath_1=fileName;
varkeyValueSplit_1=keyValueSplit;
varrecodeSplit_1=recodeSplit;
varfile_="";
varparameter=key+keyValueSplit+value;
areater[count1++]=parameter;
doReadFile();
//文件存在读取文件如果不存在创建文件
functiondoReadFile(){
//alert(9)
fileSystem.root.getFile(path_1,{
create:true
},function(f){
reader=newFileReader();
reader.onloadend=function(e){
console.log("gotoend");
//file_读取到的内容
file_=e.target.result;
alert(e.target.result)
//logit("<pre>"+e.target.result+"</pre><p/>");
};
reader.readAsText(f);
},onError);
functiononError(e){}
doAppendFile();
}
//文件不存在创建文件并写文件
functiondoAppendFile(){
alert("doAppendFile");
fileSystem.root.getFile(path_1,{
create:true
},function(f){
f.createWriter(function(writerOb){
writerOb.onwrite=function(){
//logit("Donewritingtofile.<p/>");
};
vartotal=newArray();
total=file_.split(recodeSplit_1);
varvalue_a="";
alert(areater.length);
//文件不存在第一次写
if(file_==undefined||file_==null||file_==""){
value_a=areater.join(recodeSplit_1);
}else{//文件存在
varstrs=newArray();
strs=file_.split(recodeSplit_1);
varmap=getMap();
for(i=0;i<strs.length;i++){
varstr=strs[i];
//alert(strs[i])
varkey=str.substr(0,str.indexOf(keyValueSplit_1))+"";
varvalue=str.substr(str.indexOf(keyValueSplit_1)+1)+"";
map.put(key,value);
}
for(i=0;i<areater.length;i++){
varstr=areater[i];
varkey=str.substr(0,str.indexOf(keyValueSplit_1))+"";
varvalue=str.substr(str.indexOf(keyValueSplit_1)+1)+"";
map.put(key,value);
}
varstr=map.keyset()+"";
varstr2=newArray();
str2=str.split(",");
varstr3=newArray();
for(i=0;i<str2.length;i++){
str3[i]=str2[i]+keyValueSplit_1+map.get(str2[i]);//合并key和value
}
value_a=str3.join(recodeSplit_1);
//自定义Map
functiongetMap(){
varmap_=newObject();
map_.put=function(key,value){
map_[key+'_']=value;
};
map_.get=function(key){
returnmap_[key+'_'];
};
map_.remove=function(key){
deletemap_[key+'_'];
};
map_.keyset=function(){
varret="";
for(varpinmap_){
if(typeofp=='string'&&p.substring(p.length-1)=="_"){
ret+=keyValueSplit_1;
ret+=p.substring(0,p.length-1);
}
}
if(ret==""){
returnret.split(keyValueSplit_1);
}else{
returnret.substring(1).split(keyValueSplit_1);
}
};
returnmap_;
}
}
writerOb.write(value_a);
//写成功返回确认
varrunFun=responseFun+"("+saveSuccess+")";
//字符串转对象
eval_r("("+runFun+")");
});
},onError);
functiononError(e){alert("qqq写文件内容错误:"+e);}
}
//returnwrite1;
}
//读文件中所有内容
functionreadFromFileAll(filename,keyValueSplit,recodeSplit,responseFun){
//alert(456)
//varfindValue;
varfileSystem1=null;
varpath_1=filename;
varkeyValueSplit_1=keyValueSplit;
varrecodeSplit_1=recodeSplit;
varfile_="";
varflag=1;
document.addEventListener("deviceready",onDeviceReady1,true);
//alert(1)
functiononDeviceReady1(){
//alert(2)
//requestthepersistentfilesystem
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,onFSSuccess1,onError);
}
functiononFSSuccess1(fs){//alert("aaa="+fs);
//alert(3)
fileSystem1=fs;
doReadFile();
}
functiononError(e){
alert(e);
//getById("#content").innerHTML="<h2>Error</h2>"+e.toString();
}
functiondoReadFile(){
//alert(5)
fileSystem1.root.getFile(path_1,{
create:true
},function(f){
//alert(6)
reader=newFileReader();
reader.onloadend=function(e){
console.log("gotoend");
file_=e.target.result;
//alert("9998="+e.target.result)
vartotal=newArray();
total=file_.split(recodeSplit_1);
//varjson1=newArray();
varjson1={};
//vari=0;
for(j=0;j<total.length;j++){
//alert(1)
varstr=total[j];
vars1=str.substring(0,str.lastIndexOf(keyValueSplit_1));
vars2=str.substring(str.lastIndexOf(keyValueSplit_1)+1);
json1[s1]=s2;
}
//alert(json1)
varrunFun=responseFun+"("+WMW.obj2str(json1)+")";
//alert(runFun)
eval_r("("+runFun+")");
};
reader.readAsText(f);
},onError);
functiononError(e){alert("读取文件内容错误:"+e);}
}
}
//更具KEY查找文件中值
functionreadFromFile(filename,key,keyValueSplit,recodeSplit,responseFun){
varpath_1=filename;
varkeyValueSplit_1=keyValueSplit;
varrecodeSplit_1=recodeSplit;
varfile_="";
doReadFile();
functiondoReadFile(){
//alert(doReadFile)
fileSystem.root.getFile(path_1,{
create:true
},function(f){
reader=newFileReader();
reader.onloadend=function(e){
file_=e.target.result;
//alert("9998="+e.target.result);
vartotal=newArray();
total=file_.split(recodeSplit_1);
for(j=0;j<total.length;j++){
varstr=total[j];
//alert(9)
if(str.substring(0,str.lastIndexOf(keyValueSplit_1))==key){
//alert(7)
varvalue=str.substring(str.lastIndexOf(keyValueSplit_1)+1);
//alert("value="+value);
//设置从文件中读取值
varrunFun=responseFun+"("+value+")";
//alert(runFun)
eval_r("("+runFun+")");
break;
}
}
};
reader.readAsText(f);
},onError);
functiononError(e){}
}
}
//设置从文件中读取单个值
functionsetReadFileValue(val){
varvalue=val;
//alert(value);
}
//设置从文件中读取所有值
functionsetReadFileAll(val){
alert(7)
alert("value="+val);
}
//设置从文件写成功
functionsetSaveSuccess(val){
//alert(val)
}
//测试
functiontest(){
alert("11");
onWrite3();
//readFromFile("dataFile.txt","nnn",",","\r\n");
//readFromFileAll("aaa/dataFile.txt",",","\r\n","setReadFileAll");
//save2File("aa/dataFile.txt","9999","99999999",",","\r\n");
//save2File("dataFile.txt","888888","99999999",",","\r\n");
//write2File("dataFile.txt","wwww","oooooo",",","\r\n","setReadFileAll");
//alert("aa="+aa)
}
//PhoneGap创建文件代码太过繁琐就省去了创建文件的过程,传参时只能写文件名,不能加路径
//要想把文件保存在文件夹,就先掉一下onWrite3()创建文件夹的作用。
//说这么多意思就是要把文件写在某个文件下,前提是要有这个文件,传参时才可加路径
//刚开始学,有不足之处还望指教
//文中WMW.obj2str(json1)函数没有提供,对象转换的作用,可用字符串代替直接输出
functiononWrite3(){
//alert("onWrite");
document.addEventListener("deviceready",onDeviceReady3,false);
}
functiononDeviceReady3(){
//alert("onWrite3()");
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,gotFS3,fail);
}
//获取newFile目录,如果不存在则创建该目录
functiongotFS3(fileSystem){
varfilePath="myfile";
//varfilePath=path_1.substring(0,path_1.lastIndexOf("/"));
newFile=fileSystem.root.getDirectory(filePath,{create:true,exclusive:false},writerFile3,fail);
}
functionwriterFile3(){}
functionfail(){}