Hi everyone , I’m trying to create and read string using fileUtils , although I’ve written this piece of code …the problem is that it’s not getting any string and returning null to the write file ? what’s going on ? also saved data is returning true !
if(cc.sys.isNative){
var path = jsb.fileUtils.getWritablePath();
cc.log("Path : " + JSON.stringify(path));
var data = jsb.fileUtils.getStringFromFile("itsDone.txt");
cc.log("data : " + data);
var saved = jsb.fileUtils.writeStringToFile(JSON.stringify(data) ,"imageData.txt");
cc.log(saved);
}
hi @Abulbisht,
could you please share with me how you read and wrote text to a .txt file in cocos creator?
I tried :
var filePath = “resources/input0.txt”;
var textValue = jsb.fileUtils.getStringFromFile(filePath);
to read from file, and :
var outputfilePath = “resources/output0.rtf”;
jsb.fileUtils.writeStringToFile(“hello”, outputfilePath);
to write to file.
this isn’t working.
thanks!
You guys have to write path to jsb.fileUtils.writeStringToFile() or writeToFile().
ex)
let path = jsb.fileUtils.getWritablePath();
let result = jsb.fileUtils.writeStringToFile(“developer”, path+“save.txt”); //result is boolean
let saved = jsb.fileUtils.getSTringFromFile(path+“save.txt”); // then this is the save result from you
console.log(saved) // “developer”