A Closer Look at Apollo`s File System API - File Open Modes
(Page 5 of 5 )
The FileStream.open() method and FileStream.openAsync()method both accept two parameters: thefileparameter corresponding to the file that you want to open, and thefileModeparameter, which is a string defining the capabilities of theFileStreamobject. The possible values for thefileModeparameter are defined as constants in theFileModeclass.
For example, the following code opens the file synchronously for write operations, but not for read operations:
stream.open(file, FileMode.WRITE);
Here are theFileModeconstants and their meanings:
| FileMode constant | Definition |
| FileMode.APPEND | The file is opened in write-only mode, with all written data appended to the end of the file. Upon opening, any nonexistent file is created. |
| FileMode.READ | The file is opened in read-only mode. The file must exist (missing files are not created). |
| FileMode.UPDATE | The file is opened in read/write mode, and data can be written to any position in the file or appended to the end. Upon opening, any nonexistent file is created. |
| FileMode.WRITE | The file is opened in write-only mode. If the file does not exist, it will be created. If the file does exist, it will be overwritten. |
The FileStream class includes a number of read and write methods, each corresponding to the format of the data being read or written. For example, you can use the readUTFBytes()andwriteUTFBytes()methods to read or write an array of bytes, whereas thereadByte()andwriteByte()methods read or write a single byte at a time. All in all, there are 26 read and write methods. For details on each, see the description of these methods in the ActionScript 3.0 Language Reference, which is distributed with Apollo Alpha 1.
Even though reading and writing text data may seem trivial, you should consider the encoding of the text in the file. ThereadUTFBytes()andwriteUTFBytes()methods provide means to read and write UTF-8–encoded text. ThereadMultiByte()andwriteMultiByte()methods let you specify a different character encoding for the file data. There are other factors to consider as well. For example, a UTF file may start with a UTF byte order mark (BOM) character, which defines the UTF encoding and the byte order (or “endianness”) of the data.
For more information, see the “Data formats, and choosing the read and write methods to use” section of the Apollo Developer’s Guide (http://www.adobe.com/go/apollodocs).
More Information
For examples of reading and writing files, see the following sections in Chapter 5:
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the Apollo for Adobe Flex Developer's Pocket Guide, written by Mike Chambers, Rob Dixon and Jeff Swartz (O'Reilly, 2007; ISBN: 0596513917). Check it out today at your favorite bookstore. Buy this book now.
|
|