我目前有一个可以在Samsung Tizen TV v2.4上读取和写入文件的Web应用程序(固件: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
答案 0 :(得分:1)
我认为固件中存在错误,因此附加模式无法正常工作。我测试了固件版本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)从文件中读取当前内容,附加newContents并再次写入文件。
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'工作正常。
答案 1 :(得分:0)
似乎错误甚至可以在resolve()这里,因为你说openStream()不会在日志中抛出错误。请尝试使用此代码来捕获错误消息并进行调试。
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);
}
});
答案 2 :(得分:0)
我在这里为您分享示例代码。它在我的机器上运行良好。确保在附加过程完成后尝试读取文件。
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");
});
}