0

I'm missing something. I need to write a text file for ios using NSOutputStream. I create the file

 import { knownFolders, Folder, File } from "tns-core-modules/file-system";
 const folder: Folder=<Folder>knownFolders.documents();
 let outFOS: NSOutputStream;
 let pathName: File;
 let isFileOpen: boolean;

 export class  TxtFileIo{
  constructor(fileName:string) {
    isFileOpen = false;
    try{
        pathName=<File>folder.getFile(`${fileName}.txt`);       
        console.log(`TxtFile iOS new file ${pathName.path}`);
        outFOS=<NSOutputStream> NSOutputStream.outputStreamToFileAtPathAppend(pathName.path, true)
        outFOS.open();
        }catch(IOex) {
        console.log("TxtFile ios new TxtFile error: " +IOex);
    }
    isFileOpen=true;
}

and close the file with

    close() {
     try {
        outFOS.close();
        isFileOpen=false;
        console.log("TxtFile iOS close file:" +pathName.name);
     }
     catch (IOex) {
        console.log("TxtFile iOS close error: " +IOex);
     }
   }

The file is created and is visible, however, how do I write to it. I get the above error with

 write(saveData:string) {
        try {
            console.log("TxtFile iOS write data: " +saveData);
            outFOS.write(saveData,saveData.length);
        }
        catch (IOex) {
            console.log("TxtFile iOS write error: " +IOex);
        }
    }

What am I missing? I thought 'write' was a method of NSStream.outputStream Thanks

  • 1
    `.writeLength(data, dataLength)` – Ian MacDonald Jan 22 '20 at 22:01
  • When you marshall the Objective C methods in JS, there are specific naming conventions you have to follow (refer docs). May I know why you want to write the file natively, while you can already write text files with {N} FileSystem apis? – Manoj Jan 22 '20 at 22:47
  • Ian thanks for your comment. I found the answer .writeMaxLength(data,dataLength). Why the difference I don't know. – WaspWorker Jan 22 '20 at 22:56
  • Manoj, I'm not sure why I am using native API. I do have another file to save, a more complex binary file that I will need to use native API so I just used it for the text file too. The binary file is a .fit file for Garmin/Ant/Dynastream application. Thanks for you reply – WaspWorker Jan 22 '20 at 23:00

0 Answers0