我为手表定义了一个数据库(IDBStore)。我正在尝试将其数据写入文件并清除数据库。请看下面的代码:
function writeDataLocally() {
var database = getDatabase();
var onsuccess = function(array){
var documentsDir, newFile;
tizen.filesystem.resolve("documents", onResolve, function(error) {
console.log("Could not resolve documents folder.");
console.log(error);
});
function onResolve(result) {
newFilePath = "MyFolder";
documentsDir = result;
var newDir = documentsDir.createDirectory(newFilePath);
console.log("New folder is created.");
/* ^^^^^ I can see this log ^^^^^ */
newFile = documentsDir.createFile(newFilePath + "/sensordata_" + new TimeStamp() + ".txt");
/* But the following log is not displayed! */
/* I think the error now is in here! */
console.log("New file is created.");
}
if(newFile != null) {
newFile.openStream("w", onOpenStream, function(error) {
console.log("Could not create the file.");
console(error);
}, "UTF-8");
function onOpenStream(fs) {
console.log(JSON.stringify(array));
fs.write(JSON.stringify(array));
fs.close();
console.log("Data is written into the file.");
}
}
},
onerror = function(error){
console.log(error);
};
database.getAll(onsuccess, onerror);
}
我没有收到任何日志,这表明它无法创建所需的目录。你能看看这个简单函数中的错误吗?这可能是基本的,但我是 tizen 新手。谢谢。
更新
现在我可以使用以下代码以我喜欢的方式创建我的文件夹和文件:
function writeDataLocally() {
var database = getDatabase();
var onsuccess = function(array){
var documentsDir, newFile;
tizen.filesystem.resolve("documents", onResolve, function(error) {
console.log("Could not resolve documents folder.");
console.log(error);
});
function onResolve(result) {
newFilePath = "MyFolder";
documentsDir = result;
var newDir = documentsDir.createDirectory(newFilePath);
console.log("New folder is created.");
d = new Date();
newFile = newDir.createFile("sensordata_" + d.toDateString() + ".txt");
console.log("New file is created.");
if(newFile != null) {
newFile.openStream("w", onOpenStream, function(error) {
console.log("Could not create the file.");
console(error);
}, "UTF-8");
function onOpenStream(fs) {
console.log("File is opened and ready to write...");
fs.write(JSON.stringify(array));
fs.close();
newFile = null;
console.log("Data is written into the file");
};
}
},
onerror = function(error){
console.log(error);
};
database.getAll(onsuccess, onerror);
}
查看newDir
'newFile` 的更改。但现在的情况是:
如果文件夹存在,什么都不会发生!从设备中删除myFolder后,文件和文件夹都会创建,并将数据写入文件,但只是第一次。
如果设备中已有myFolder文件夹,则不会在该目录中创建其他文件。建议?