Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

18. File > Writing Files

Writing Files

To write data to files in either persistent or temporary storage, an application uses a FileWriter object. To begin the process, the application must first get access to a file object representing the file using the getFile method, as shown in the following example:

theFileSystem.root.getFile('appdata1.txt', {create : true},
  onGetFileSuccess, onFileError);

After the call to getFile, the onGetFileSuccess function is executed and passed the file object that will be used to create the FileWriter, as shown in the following example:

function onGetFileSuccess(theFile) {
  theFile.createWriter(onCreateWriterSuccess, onFileError);
}

Once again, we have another callback to wait for; once the FileWriter has been created, the callback function is executed, and the actual file writing can happen, as illustrated in the following example:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial