O'Reilly logo

Apollo for Adobe Flex Developers Pocket Guide by Jeff Swartz, Rob Dixon, Mike Chambers

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 5. Apollo Mini-Cookbook

This chapter describes solutions to common tasks in Apollo applications. The solutions in this chapter illustrate many concepts described in previous chapters, and it provides working MXML code that you can test using Flex Builder or the Apollo SDK.

Working with the File System

Apollo lets you access the file system on the user’s computer. This section of the cookbook shows some solutions to common tasks when working with the file system.

For a conceptual overview of the Apollo file system capabilities, see “Accessing the File System.”

Writing a Text File from a String

Problem

You want to synchronously write data from an ActionScript String object to a text file.

Solution

Call an appropriate write method, such as writeUTF( ), of a FileStream object.

Discussion

This example uses a File object named file, which represents a test.txt file in the Apollo Test subdirectory of the user’s documents directory:

var file:File = File.documentsDirectory
file = file.resolve("Apollo Test/test.txt");

For more information, see “Accessing the File System.”

The FileStream class is used to open, read, write, and close files. You pass a fileMode parameter to the open( ) method (or to the openAsync( ) method, if you want the operation to occur asyncronously) to specify the capabilities of the FileStream object. In this case, FileMode.WRITE provides write access to the file:

var stream:FileStream = new FileStream( )
stream.open(file, FileMode.WRITE);

Tip

The FileMode.APPEND and FileMode.UPDATE ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required