在我的函数中,我想按照这些说明打开新创建的文件并将我的数据写入其中。这是我的代码:
function writeDataToFile(newFile) {
try {
console.log("[Matin] writeDataToFile started...");
if(newFile != null) {
newFile.openStream('a', onOpenStream, function(error) {
console.log("[Matin] Could not create the file.");
console(error);
}, "UTF-8");
function onOpenStream(fs) {
console.log("[Matin] New file is opened.");
fs.write("Hello ---------- Data Goes Here ----------");
fs.write(JSON.stringify(dataToFile));
console.log("[Matin] this is the data to be written>>>\n" + JSON.stringify(dataToFile));
fs.close();
dataToFile = null;
newFile = null;
console.log("[Matin] Data is written into the file, and temporal variables are set to null.");
};
} else {
console.log("[Matin] no file here to write into!...");
}
console.log("[Matin] writeDataToFile ended!!!");
} catch (exception) {
console.log("[Matin] [Exception] " + exception.message);
}
}
这是我从执行函数中得到的日志。一切看起来都很好:
js/managers/exportManager.js (295) :[Matin] writeDataToFile started...
js/managers/exportManager.js (314) :[Matin] writeDataToFile ended!!!
js/managers/exportManager.js (303) :[Matin] New file is opened.
js/managers/exportManager.js (305) :[Matin] this is the data to be written>>>
[{"steps":null,"heartrate":null,"accelX":"-0.9709117078781128","accelY":"3.4513116836547852","accelZ":"8.347122440338135",...]
js/managers/exportManager.js (309) :[Matin] Data is written into the file, and temporal variables are set to null.
但问题是,当我查看文件时,并没有写入任何内容!该文件中没有数据。连线都不行Hello ---------- Data Goes Here ----------
。可能是什么原因?谢谢。
更新
该功能运行良好,文件已正确保存。但是,需要在手表上重新启动,以便您可以读取文件。否则,它将向您显示空文件。