Peter I
Peter I

Reputation: 942

tizen.filesystem FileSystem append issue

I currently have a web application that can read and write files on a Samsung Tizen TV v2.4 (Firmware: T-HKMLAKUC-1006.4)

Append hover does not seem to work.

I can do the following with no issues at all:

tizen.filesystem.resolve('documents/log.txt', (file) => {
    file.openStream("w", (fs) => {

        fs.write('Tizen .. '); // Works

        console.log('written to log Tizen ... '); // View if its running the code block

        // This block is ran as i get the console log from this function

    }, (e) => {

        console.log("Error " + e.message);
        // No Errors thrown

    }, "UTF-8");
}, (e) => {

    // No Errors thrown

}, "a");

If i change the 'w' to 'a' which should append to the file the file is empty

tizen.filesystem.resolve('documents/log.txt', (file) => {
    file.openStream("a", (fs) => {

        console.log('written to log Tizen ... '); // View if its running the code block

        // This block is ran as i get the console log from this function

        fs.write('Tizen .. '); // When i read it nothing here

    }, (e) => {
        console.log("Error " + e.message);
    }, "UTF-8");
});

Has anyone else seen this issue? Thanks

If i monitor the console logs from this function i get the following:

4:59:21 pm | YaBRm3kxLCctUNmhl034oLe0QQA= | "" // View the file

4:59:21 pm | YaBRm3kxLCctUNmhl034oLe0QQA= | "running append to file" // Run the append function
4:59:21 pm | YaBRm3kxLCctUNmhl034oLe0QQA= | "written to log Tizen ... " // Append function shows its running the success block

4:59:22 pm | YaBRm3kxLCctUNmhl034oLe0QQA= | "Tizen .. " // View the file (and its appended)

4:59:23 pm | YaBRm3kxLCctUNmhl034oLe0QQA= | "running append to file" // Run the append function again
4:59:23 pm | YaBRm3kxLCctUNmhl034oLe0QQA= | "written to log Tizen ... " // Shows its running the append function correctly
4:59:24 pm | YaBRm3kxLCctUNmhl034oLe0QQA= | "" // View the file and its empty

Upvotes: 0

Views: 552

Answers (3)

AndreyNik
AndreyNik

Reputation: 1631

I think the bug in firmware, so append mode doesn't work correctly. I tested on firmware version T-HKMLAKUC-1008.2, the same error reproduce. My workaround, read current content from file, merge with new content and write again to file.

1) Create file

  function createFile() {
        tizen.filesystem.resolve('downloads', (dir) => {
            var tizenFile = dir.createFile('/2017_10_08_13_13_21_826.log');
        }, (error) => {
            console.error("error resolve dir", error);
        }, 'rw');
    }

2) Read current content from file, append with newContents and write again to file.

function appendToFile(newContents) {
    tizen.filesystem.resolve('downloads/2017_10_08_13_13_21_826.log', (tizenFile) => {
        tizenFile.readAsText((currentContent) => {
            var writeContents = currentContent + newContents;
            tizenFile.openStream('w', (stream) => {
                stream.write(writeContents);
                stream.close();
            }, (error) => {
                console.error("error open stream content", error);
            }, 'UTF-8');
        }, (error) => {
            console.error("error read current content", error);
        }, 'UTF-8')
    }, (error) => {
        console.error("error resolve log file", error);
    }, 'r');
}

Mode 'w' works correctly.

Upvotes: 1

Md. Armaan-Ul-Islam
Md. Armaan-Ul-Islam

Reputation: 2184

I am sharing a sample code for you here. It worked fine on my machine. Make sure you are trying to read the file once append process is completed.

function createFile(){// Create file:   
    var newDir, newFile;

    tizen.filesystem.resolve("documents", function(dir) 
            {
               newDir = dir.createDirectory("myFDir3");     //Create new directory
               newFile = newDir.createFile("myFile.txt");   //Create new File
            },
        function(e) {
                console.log("Error " + e.message);
            }
    );
}

function writeToFile(){
    //Write to File
    var fileW;
    tizen.filesystem.resolve("documents", function(dir) 
        {
           fileW = dir.resolve("myFDir3/myFile.txt");
           fileW.openStream( "w", function(fs) {
                 fs.write("test write");
                 fs.close();
                 console.log("Write Successful");
            }, function(e) {
                 console.log("Error " + e.message);
            }, "UTF-8");
        });
}

function appendToFile(){
     //Append to File
       var fileA;
       tizen.filesystem.resolve("documents", function(dir) 
           {
              fileA = dir.resolve("myFDir3/myFile.txt");
              fileA.openStream( "a", function(fs) {
                 fs.write(" and test write again");
                 fs.close();
                 console.log("Append Successful");
               }, function(e) {
                 console.log("Error " + e.message);
               }, "UTF-8");
           });
    }

function readFromFile(){
    // Read from file:
    var fileR;  
    tizen.filesystem.resolve("documents", function(dir) 
        {
           fileR = dir.resolve("myFDir3/myFile.txt");
           fileR.openStream("r", function(fs) {
                    var text = fs.read(fileR.fileSize);
                    fs.close();
                    console.log(text);
                }, function(e) {
                    console.log("Error " + e.message);
                }, "UTF-8");
        });
}

Upvotes: 0

Md. Armaan-Ul-Islam
Md. Armaan-Ul-Islam

Reputation: 2184

Seems the error could even be with resolve() here as you are saying openStream() is not throwing error in log . Try this code instead to catch the error message and debug thereby.

tizen.filesystem.resolve('documents/log.txt', (file) => {
    file.openStream("a", (fs) => {
        fs.write('Tizen .. '); // When i read it nothing here
    }, (e) => {
        console.log("Error at openStream:" + e.message);
    }, "UTF-8");
    , 
    function(e) {
     console.log("Error at resolve:" + e.message);
   }
}); 

Upvotes: 0

Related Questions