HTML and CSS Reference
In-Depth Information
Note The then method is executed as soon as the previous function has either successfully
completed or has reached an error. The done function is the same, except that it is guaran-
teed to throw any error that is not handled inside the function.
Appending text versus just writing text
When you use the writeTextAsync method, Windows 8 just opens the file, writes any content, and then
closes it. If you call writeTextAsync twice, then the content is overwritten; the second call completely
wipes out the existing content.
If you want to just append text to an existing file, you have to use the appendTextAsync method.
The method has the same syntax as the writeTextAsync method. In the previous code snippet, you see
an example of the two methods used sequentially.
Because of the asynchronous API, however, you might want to keep sequential tasks to a minimum
to prevent your code from reaching an unmanageable level of nesting. As far as file writing is
concerned, you might always want to focus on writing any content to files in a single step.
Note If you explore the list of methods out of the Windows 8 file object, you will find
methods to rename, copy, delete, and move files. You won't find methods to read or write
any content to a file exposed on the file object itself. For reading and writing files, you must
always use methods on the Windows.Storage.FileIO object.
Deleting files
Another common operation that an application may perform is deleting files it had previously created
or, more in general, any files that, for some reason, it needs to delete. As noted a moment ago, file
deletion is an operation that requires a file object to be performed. Here's an example where you
assume to have the file object available:
.done(function () { TodoList.alert("File deleted" },
function () { TodoList.alert("Unable to delete the file" });
Choosing a serialization format
Now that you have grabbed the basics of file manipulation in Windows 8, let's return to the TodoList
application. You integrated file saving in the code, but you didn't actually save any task yet. The code
discussed earlier, in fact, created a file with a given name that was limited to store sample text in it.
The next step is therefore choosing a format to store real data to a file. The process of streamlining
real data to a file is commonly referred to as serialization .
Search WWH ::

Custom Search