|

楼主 |
发表于 2016-3-31 17:32:57
|
显示全部楼层
我用文件进行存储操作,但是,删除APP后重新安装也是一样。而且文件读写操作是正常的,在手机目录中找不到我创建的目录,各位高手,帮忙看看原因何在!
-----------main.js引用如下:
var configPath = "myHello586";
var configFile = "myHello.config";
fileHelper.write('orange',configPath,configFile);
fileHelper.read(configPath+'/'+configFile);
------------------fileHelper.js定义如下:
define(function(require){
var _data="";
var _directory="";//default directory
var _fileName="";//default file name
var _filePath="";
var _text="";//读取的数据
require("cordova!cordova-plugin-file");
var fileHelper={};
//写操作
fileHelper.write = function(data,directory,fileName){
_data=data;
_directory=directory;
_fileName=fileName;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
//获取目录,如果不存在则创建该目录
function onFileSystemSuccess(fileSystem) {
newFile = fileSystem.root.getDirectory(_directory, {
create : true,
exclusive : false
}, onDirectorySuccess, onFileSystemFail);
}
//取文件,不存在则创建
function onDirectorySuccess(newFile) {
newFile.getFile(_fileName, {
create : true,
exclusive : false
}, onFileSuccess, onFileSystemFail);
}
/**
* 获取FileWriter对象,用于写入数据
* @param fileEntry
*/
function onFileSuccess(fileEntry) {
fileEntry.createWriter(onFileWriteSuccess, onFileSystemFail);
}
//写操作
function onFileWriteSuccess(writer) {
//log("fileName="+writer.fileName+";fileLength="+writer.length+";position="+writer.position);
writer.onwrite = function(evt) {//当写入成功完成后调用的回调函数
};
writer.onerror = function(evt) {//写入失败后调用的回调函数
alert("文件写入出错!");
};
writer.onabort = function(evt) {//写入被中止后调用的回调函数,例如通过调用abort()
alert("文件写入终止!");
};
// 快速将文件指针指向文件的尾部 ,可以append
writer.seek(writer.length);
writer.write(_data);//向文件中写入数据
}
function onFileSystemFail(error) {
alert("文件操作错误:" + error.code);
}
};
//读操作
//path为文件全路径
fileHelper.read = function(filePath)
{
_filePath = filePath;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(_filePath, {
create : true,
exclusive : false
}, onFileSuccess, onFileSystemFail);
}
function onFileSuccess(fileEntry) {
fileEntry.file(onReadText, onFileSystemFail);
alert(fileEntry.fullPath);
}
function onReadText(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
_text=evt.target.result;//将读取到的数据赋值给变量
if(_text==null||_text.length==0){
_text="没有数据!";
}
alert(_text);
};
reader.readAsText(file);
}
function onFileSystemFail(error) {
alert("文件读取出错:" + error.code);
}
};
return fileHelper;
});
|
|