我为手表定义了一个数据库(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);
}
我没有收到任何日志,表示无法创建所需的目录。你能看到这个简单功能的错误吗?这可能是基本的,但我是新来的。感谢。
<小时/> <小时/> 的更新
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 文件夹,则该目录中不会创建其他文件。建议?
答案 0 :(得分:1)
您需要在应用的config.xml文件中添加以下权限:
<tizen:privilege name="http://tizen.org/privilege/filesystem.write"/>
<tizen:privilege name="http://tizen.org/privilege/filesystem.read"/>
然后您就可以创建目录/文件了。
修改强>
我认为您的代码中存在拼写错误。
documentsDir = result;
var newDir = docuemntsDir.createDirectory(...
应该是,
documentsDir = result;
var newDir = documentsDir.createDirectory(...
EDIT2
尝试以下两种方法:
方法1 -
newFile = documentsDir.createFile("sensordata.txt");
方法2 -
var timestamp = new TimeStamp();
newFile = documentsDir.createFile(newFilePath + "sensordata_" + timestamp + ".txt");
如果两种方法都有效,那么采取&#34;方法2&#34;为了你的代码。
<强> EDIT3 强>
回调函数onResolve(),初始化&#34; newFile&#34; var,你试图在它之外访问它可能尚未初始化的地方。
将下面的代码放在onResolve()函数
中if(newFile != null) {
newFile.openStream("w", onOpenStream, function(error) {
console.log("Could not create the file.");
console(error);
}, "UTF-8");
此外,onResolve函数中存在拼写错误,代码
newFile = newDir.createFile("sensordata_" + d.toDateString() + ".txt");
应该是
newFile = documentsDir.createFile("sensordata_" + d.toDateString() + ".txt");
您的代码中还有一个问题,即您无法将变量direclty传递给createDirectory
var newDir = documentsDir.createDirectory(newFilePath);
请按原样使用字符串,即
var newDir = documentsDir.createDirectory("MyFolder");
<强> EDIT4 强> 基本上你需要先尝试解析你创建的文件夹,如果它存在,那么就不需要创建一个新的文件夹了。
从onsuccess中删除此代码
var documentsDir, newFile;
tizen.filesystem.resolve("documents", onResolve, function(error) {
console.log("Could not resolve documents folder.");
console.log(error);
});
并在其位置添加以下代码。
var documentsDir, newFile;
tizen.filesystem.resolve("MyFolder", onMyFolderResolve, function(error) {
console.log("Your MyFolder doesn't exists.");
console.log("Now resolve documents folder");
tizen.filesystem.resolve("documents", onResolve, function(error){console.log("Could not resolve documents folder");});
});
function onMyFolderResolve(result) {
console.log("Your folder exists");
myfolderDir = result;
d = new Date();
newFile = myfolderDir.createFile("sensordata_" + d.toDateString() + ".txt");
}