tizen.filesystem 文件系统追加问题

tizen.filesystem FileSystem append issue

我目前有一个网络应用程序可以在 Samsung Tizen TV v2.4(固件:T-HKMLAKUC-1006.4)上读取和写入文件

附加悬停似乎不起作用。

我可以毫无问题地执行以下操作:

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

如果我将 'w' 更改为 'a',这应该附加到文件中,文件为空

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

还有其他人看到过这个问题吗?谢谢

如果我从此函数监视控制台日志,我会得到以下信息:

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

似乎错误甚至可能与这里的 resolve() 有关,因为你说 openStream() 没有在 log 中抛出错误。请尝试使用此代码来捕获错误消息并进行调试。

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

我在这里为您分享一个示例代码。它在我的机器上运行良好。确保您在附加过程完成后尝试读取文件。

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

我认为是固件中的错误,所以附加模式无法正常工作。我在固件版本 T-HKMLAKUC-1008.2 上测试,同样的错误重现。我的解决方法是从文件中读取当前内容,与新内容合并并再次写入文件。

1) 创建文件

  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) 从文件中读取当前内容,追加新内容并再次写入文件。

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

模式 'w' 工作正常。