Packageflash.filesystem
Classpublic class FileStream
InheritanceFileStream Inheritance Object

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0

A FileStream object is used to read and write files. Files can be opened synchronously by calling the open() method or asynchronously by calling the openAsync() method.



Public Methods
 MethodDefined By
  
Creates a FileStream object.
FileStream
 Inherited
_init():*
[static]
_init
  
AIR-only close():void
Closes the FileStream object.
FileStream
 Inherited
AS3
 Inherited
init():*
[static]
init
 Inherited
AS3
  
AIR-only open(file:File, fileMode:String):void
Opens the FileStream object synchronously, pointing to the file specified by the file parameter.
FileStream
  
AIR-only openAsync(file:File, fileMode:String):void
Opens the FileStream object asynchronously, pointing to the file specified by the file parameter.
FileStream
 Inherited
AS3
  
AIR-only truncate():void
Truncates the file at the position specified by the position property of the FileStream object.
FileStream
Protected Methods
 MethodDefined By
 Inherited
[static]
protected
 Inherited
_setPropertyIsEnumerable(o:*, V:String, enumerable:Boolean):void
[static]
protected
Public Constants
 ConstantDefined By
 Inheritedlength : int = 1
[static]
length
Constructor Detail
AIR-only FileStream()Constructor
public function FileStream()

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0

Creates a FileStream object. Use the open() or openAsync() method to open a file.

Method Detail
AIR-only close()method
public function close():void

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0

Closes the FileStream object.

AIR-only open()method 
public function open(file:File, fileMode:String):void

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0

Opens the FileStream object synchronously, pointing to the file specified by the file parameter.

If the FileStream object is already open, calling the method closes the file before opening and no further events (including close) are delivered for the previously opened file.

On systems that support file locking, a file opened in "write" or "update" mode (FileMode.WRITE or FileMode.UPDATE) is not readable until it is closed.

Once you are done performing operations on the file, call the close() method of the FileStream object. Some operating systems limit the number of concurrently open files.

Parameters

file:File — The File object specifying the file to open.
 
fileMode:String — A string from the FileMode class that defines the capabilities of the FileStream, such as the ability to read from or write to the file.


Throws
IOError — The file does not exist; you do not have adequate permissions to open the file; you are opening a file for read access, and you do not have read permissions; or you are opening a file for write access, and you do not have write permissions.
 
SecurityError — The file location is in the application directory, and the fileMode parameter is set to "append", "update", or "write" mode.

Example

The following code shows how to synchronously open a test.txt file in the Apollo Test subdirectory of the user's documents directory and then read the file into a string, using the system character set as the text encoding.

         import flash.filesystem.*;
         
         var file:File = File.documentsDirectory;
         file = file.resolvePath("Apollo Test/test.txt");
         var fileStream:FileStream = new FileStream();
         fileStream.open(file, FileMode.READ);
         var str:String = fileStream.readMultiByte(file.size, File.systemCharset);
         trace(str);
         fileStream.close();
         
         
AIR-only openAsync()method 
public function openAsync(file:File, fileMode:String):void

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0

Opens the FileStream object asynchronously, pointing to the file specified by the file parameter.

Parameters

file:File — The File object specifying the file to open.
 
fileMode:String — A string from the FileMode class that defines the capabilities of the FileStream, such as the ability to read from or write to the file.

AIR-only truncate()method 
public function truncate():void

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0

Truncates the file at the position specified by the position property of the FileStream object.

Bytes from the position specified by the position property to the end of the file are deleted. The file must be open for writing.


Throws
IllegalOperationError — The file is not open for writing.

Example

The following code synchronously opens a test.txt file in the Apollo Test subdirectory of the user's documents directory and then trims the file to 100 characters in length if it is longer than 100 characters.

         import flash.filesystem.*;
         
         var file:File = File.documentsDirectory;
         file = file.resolvePath("Apollo Test/test.txt");
         var fileStream:FileStream = new FileStream();
         fileStream.open(file, FileMode.UPDATE);
         if (file.size > 100) {
             fileStream.position = 100;
             fileStream.truncate();
         }
         fileStream.close();
         
         

The following code asynchronously opens a test.txt file in the Apollo Test subdirectory of the user's documents directory, and then trims the file to 100 characters in length if it is longer than 100 characters.

         import flash.filesystem.*;
         
         var file:File = File.documentsDirectory;
         file = file.resolvePath("Apollo Test/test.txt");
         var fileStream:FileStream = new FileStream();
         fileStream.openAsync(file, FileMode.UPDATE);
         trace("start", file.size);
         if (file.size > 100) {
             fileStream.position = 100;
             fileStream.truncate();
         }
         fileStream.addEventListener(Event.CLOSE, fileClosed);
         fileStream.close();
         function fileClosed(event:Event):void {
             trace("closed", file.size);
         }