Creating and writing into a file in Gear S

瘦欲@ 提交于 2019-12-13 04:59:49

问题


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

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

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?


回答1:


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.

EDIT

I think there is a typo in your code.

documentsDir = result;
var newDir = docuemntsDir.createDirectory(...

It should be,

documentsDir = result;
var newDir = documentsDir.createDirectory(...

EDIT2

Try below two methods:

Method 1 -

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

Method 2 -

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

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

EDIT3

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

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

Also, there is typo in onResolve function, below code

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

should be

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

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 Basically you need to first trying resolving your created folder i.e if it exists then no need to create a new folder.

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


来源:https://stackoverflow.com/questions/30631643/creating-and-writing-into-a-file-in-gear-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!