0

我为手表定义了一个数据库(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文件夹,则不会在该目录中创建其他文件。建议?

4

1 回答 1

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(...

编辑2

尝试以下两种方法:

方法1 -

newFile = documentsDir.createFile("sensordata.txt");

方法2 -

var timestamp = new TimeStamp();
newFile = documentsDir.createFile(newFilePath + "sensordata_" + timestamp + ".txt");

如果两种方法都有效,则为您的代码采用“方法 2”。

编辑3

回调函数 onResolve(),初始化“newFile”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");

您的代码中还有一个问题,您不能将变量直接传递给 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");
}
于 2015-06-04T13:21:16.910 回答