在 Gear S 中创建和写入文件 [英] Creating and writing into a file in Gear S

查看:39
本文介绍了在 Gear S 中创建和写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为手表定义了一个数据库 (IDBStore).我正在尝试将其数据写入文件并清除数据库.请看下面的代码:

I have a database (IDBStore) defined for the watch. I am trying to write its data into a file and clear the database. Please see the code below:

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 的新手.谢谢.

I am not getting any logs, which says it cannot create the required directory. Can you please see the bugs in this simple function? It may be basic, but I am new to tizen. Thanks.



更新
现在我可以使用以下代码以我喜欢的方式创建我的文件夹和文件:



UPDATE
Now I am able to create my folder and file in a way I like using the following code:

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 文件夹,则不会在该目录中创建其他文件.建议?

See the changes for newDir and 'newFile`. But here is the situation now:
If the folder exists, nothing is going to happen! Once I delete myFolder from the device, both file and folder are created and data is written into the file, but only for the first time.
If there is already a myFolder folder in the device, no other file is created in that directory. Suggestions?

推荐答案

您需要在应用的 config.xml 文件中添加以下权限:

You need to add the following privileges in your app's config.xml file:

<tizen:privilege name="http://tizen.org/privilege/filesystem.write"/>
<tizen:privilege name="http://tizen.org/privilege/filesystem.read"/> 

然后你就可以创建目录/文件了.

Then you will be able to create directory/file.

编辑

我认为您的代码中存在拼写错误.

I think there is a typo in your code.

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

If both method works, then take "Method 2" for your code.

EDIT3

回调函数 onResolve(),初始化newFile"var,并且您试图在它可能尚未初始化的地方访问它.

The callback function onResolve(), initialize "newFile" var, and you are trying to access outside it where it might have not been initialized.

将下面的代码放在 onResolve() 函数中

Put below code inside onResolve() function

if(newFile != null) {
            newFile.openStream("w", onOpenStream, function(error) {
                console.log("Could not create the file.");
                console(error);
            }, "UTF-8");

另外,onResolve 函数有错别字,代码如下

Also, there is typo in onResolve function, below code

newFile = newDir.createFile("sensordata_" + d.toDateString() + ".txt");

应该是

newFile = documentsDir.createFile("sensordata_" + d.toDateString() + ".txt");

您的代码中还有一个问题,您无法将变量 direclty 传递给 createDirectory

One more problem is there in your code, you can't pass a variable direclty to createDirectory

var newDir = documentsDir.createDirectory(newFilePath);

请按原样使用字符串,即

Kindly use the string as it is, i.e

var newDir = documentsDir.createDirectory("MyFolder");

EDIT4基本上,您需要首先尝试解析您创建的文件夹,即如果它存在,则无需创建新文件夹.

EDIT4 Basically you need to first trying resolving your created folder i.e if it exists then no need to create a new folder.

从 onsuccess 中删除此代码

Remove this code from onsuccess

var documentsDir, newFile;
        tizen.filesystem.resolve("documents", onResolve, function(error) {
            console.log("Could not resolve documents folder.");
            console.log(error);
        });

并在其位置添加以下代码.

And add below code in its place.

            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");
}

这篇关于在 Gear S 中创建和写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆