B.3. Chapter 8 Exercise

This exercise asked you to create a new file and set its contents to a specific string. To do this, you will need to use both the File class for creating the file and the FileStream class to write out the contents of the file. If you look at Listing B-3, you will see that there is a single function named createFile(). This function first uses the File class to create a file named Today.txt on the desktop. It then creates the string in a variable named s, which was requested per the instructions by using the Date class to capture today's date. Finally, using the FileStream class, the new file is opened in Write mode, and the contents of the s variable are written to the file. The results can be seen in Figures B-3 and B-4.

Example B-3. The solution for the Chapter 8 Exercise
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml">
<!--
    Create a new file on the desktop named Today.txt and set its contents to
    be "Today is (insert the date using the Date class) and I have
    created my first file from AIR."
    -->
    <mx:Script>
        <![CDATA[
        import mx.controls.Alert;
        private function createFile():void{
            var newFile:File = File.desktopDirectory.resolvePath("Today.txt");
            var d:Date = new Date();
            var s:String = "Today is " + d + "" +
                           " and I have created my first file from AIR.";
            var fileStream:FileStream = new FileStream(); fileStream.open(newFile, FileMode.WRITE); fileStream.writeUTFBytes(s); fileStream.close(); mx.controls.Alert.show( ...

Get Beginning Adobe® AIR™: Building Applications for the Adobe Integrated Runtime now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.