Advanced:DCloud{一、本地存储文件}
2019-08-15 本文已影响0人
Codeismylife
写入文件
document.getElementById("but1").onclick = function(){
plus.io.requestFileSystem(plus.io.PRIVATE_DOC,function(fs){
fs.root.getFile("fanya/fy"+".json",
{create:true},
function(fileEntry){
fileEntry.file(function(file){
fileEntry.createWriter(
function(writer){
writer.write(JSON.stringify([{id:100,url:'baidu.url'},{id:250,url:'baidu.url'}]));
});
});
})
})
}
读取文件
document.getElementById("but2").onclick = function(){
plus.io.resolveLocalFileSystemURL( "/storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/fanya/fy.json", function( entry ) {
entry.file( function(file){
var fileReader = new plus.io.FileReader();
fileReader.readAsText(file, 'utf-8');
fileReader.onloadend = function(evt) {
var arr = JSON.parse(evt.target.result),str = "";
for(var i = 0; i<arr.length;i++){
str+=`
<li>id:${arr[i].id}; url:${arr[i].url}</li>
`
}
document.getElementById("box").innerHTML = str;
}
} );
});
}