我目前有一个web应用程序,可以在三星Tizen TV 2.4(固件: that 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发布于 2017-10-08 10:47:45
我认为固件中的错误,所以附加模式不能正常工作。我测试了固件版本the 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”工作正常。
发布于 2017-05-04 05:13:43
在这里,如果您说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);
}
}); 发布于 2017-05-11 06:36:16
我在这里为您分享一个示例代码。它在我的机器上运行得很好。一旦附加过程完成,请确保正在尝试读取该文件。
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");
});
}https://stackoverflow.com/questions/43680220
复制相似问题