As3 open file air


















The default value of this property is infinity: by default a file that is opened to read asynchronously reads as far as the end of the file. Reading data from the read buffer does not change the value of the readAhead property.

When you read data from the buffer, new data is read in to refill the read buffer. As data is read in asynchronously, the FileStream object dispatches progress events. In the event handler method for the progress event, check to see that the required number of bytes is available by checking the bytesAvailable property , and then read the data from the read buffer by using a read method.

Creates a FileStream object. Use the open or openAsync method to open a file. Closes the FileStream object. You cannot read or write any data after you call the close method. If the file was opened asynchronously the FileStream object used the openAsync method to open the file , calling the close method causes the object to dispatch the close event. Closing the application automatically closes all files associated with FileStream objects in the application.

However, it is best to register for a closed event on all FileStream objects opened asynchronously that have pending data to write, before closing the application to ensure that data is written. You can reuse the FileStream object by calling the open or the openAsync method. This closes any file associated with the FileStream object, but the object does not dispatch the close event. For a FileStream object opened asynchronously by using the openAsync method , even if you call the close event for a FileStream object and delete properties and variables that reference the object, the FileStream object is not garbage collected as long as there are pending operations and event handlers are registered for their completion.

In particular, an otherwise unreferenced FileStream object persists as long as any of the following are still possible:. 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.

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. Opens the FileStream object asynchronously, pointing to the file specified by the file parameter. If the fileMode parameter is set to FileMode. UPDATE , AIR reads data into the input buffer as soon as the file is opened, and progress and open events are dispatched as the data is read to the input buffer.

Reads a Boolean value from the file stream, byte stream, or byte array. A single byte is read and true is returned if the byte is nonzero, false otherwise.

Reads the number of data bytes, specified by the length parameter, from the file stream, byte stream, or byte array. The bytes are read into the ByteArray objected specified by the bytes parameter, starting at the position specified by offset. The default value of 0 causes all available data to be read.

Reads an IEEE double-precision floating point number from the file stream, byte stream, or byte array. Reads an IEEE single-precision floating point number from the file stream, byte stream, or byte array. Reads a multibyte string of specified length from the file stream, byte stream, or byte array using the specified character set.

Parameters length : uint — The number of bytes from the byte stream to read. Possible character set strings include "shift-jis" , "cn-gb" , "iso" , and others. For a complete list, see Supported Character Sets. For example, a value for the charSet parameter, as in myTest. Reads an object from the file stream, byte stream, or byte array, encoded in AMF serialized format. Reads a UTF-8 string from the file stream, byte stream, or byte array.

The string is assumed to be prefixed with an unsigned short indicating the length in bytes. Reads a sequence of UTF-8 bytes from the byte stream or byte array and returns a string. Parameters length : uint — The number of bytes to read. 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. Writes a Boolean value. A single byte is written according to the value parameter, either 1 if true or 0 if false. Parameters value : Boolean — A Boolean value determining which byte is written.

If the parameter is true , 1 is written; if false , 0 is written. Writes a byte. The low 8 bits of the parameter are used; the high 24 bits are ignored. Parameters value : int — A byte value as an integer.

Writes a sequence of bytes from the specified byte array, bytes , starting at the byte specified by offset using a zero-based index with a length specified by length , into the file stream, byte stream, or byte array.

If the length parameter is omitted, the default length of 0 is used and the entire buffer starting at offset is written. If the offset parameter is also omitted, the entire buffer is written. If the offset or length parameter is out of range, they are clamped to the beginning and end of the bytes array. Parameters bytes : ByteArray — The byte array to write. Parameters value : Number — A double-precision bit floating point number. Parameters value : Number — A single-precision bit floating point number.

Parameters value : int — A byte value as a signed integer. Writes a multibyte string to the file stream, byte stream, or byte array, using the specified character set. Parameters value : String — The string value to be written. Writes an object to the file stream, byte stream, or byte array, in AMF serialized format. Writes a bit integer. SecurityError — The caller is not in the application security sandbox. More examples Modifying File paths. The following code shows the difference between the nativePath property and the url property of a File object.

The comments show results on an example Windows computer. Implementation public function get parent : File Example How to use this example The following code uses the parent property to show the directory that contains a temporary file. Implementation public function get preventBackup : Boolean public function set preventBackup value: Boolean : void. Implementation public static function get separator : String Learn more Getting file system information.

The following code uses the getRelativePath method to get the relative path between a directory and a file.

The code then uses the File. Implementation public function get spaceAvailable : Number More examples Determining space available on a volume. Implementation public static function get systemCharset : String Learn more Getting file system information. The following code opens a file a test. The following code outputs a list of files and directories contained in the root level of the user directory: Copy. Pointing a File object to a file. Events cancel : Event — Dispatched when the user clicks the Cancel button in the Open File dialog box.

SecurityError — The application does not have the necessary permissions. More examples Letting the user browse to select a directory. When the directory is selected, the code lists the contents of the selected directory in the trace output.

More examples Letting the user browse to select a file. When the file is selected, the code reads the file data into a string. When the files are selected, the code outputs the paths for the selected files. Events cancel : Event — Dispatched when the user clicks the Cancel button in the Save File dialog box.

When the files are selected, the code saves data to the selected file path. In addition, canonicalization converts short filenames to long filenames on Windows. More examples Obtaining canonical versions of file names. The following code shows how to use the canonicalize method to find the correct capitalization of a directory name. Before running this example, create a directory named AIR Test on the desktop of your computer. The following code shows how to use the canonicalize method to find the long name of a Windows directory based on its short name.

Returns File. Throws IOError — The source does not exist; or the destination exists and overwrite is false ; or the source could not be copied to the target; or the source and destination refer to the same file or folder and overwrite is set to true. On Windows, you cannot copy a file that is open or a directory that contains a file that is open.

SecurityError — The application does not have the necessary permissions to write to the destination. More examples Copying and moving directories Copying and moving files.

The following code shows how to use the copyTo method to copy a file. Before running this code, create a test1. The resulting copied file is named test2.

When you set the overwrite parameter to true , the operation overwrites any existing test2. The try and catch statements show how to respond to errors. Events complete : Event — Dispatched when the file or directory has been successfully copied.

Throws SecurityError — The application does not have the necessary permissions to write to the destination. More examples Copying and moving files. The following code shows how to use the copyToAsync method to copy a file.

Before running this code, be sure to create a test1. Throws IOError — The directory did not exist and could not be created. More examples Creating directories. The following code moves a file named test. The call to the createDirectory method ensures that the AIR Test directory exists before the file is moved.

Returns File — A File object referencing the new temporary directory. More examples Creating a temporary directory. The following code uses the createTempFile method to obtain a reference to a new temporary directory.

Returns File — A File object referencing the new temporary file. More examples Creating a temporary file. The following code uses the createTempFile method to obtain a reference to a new temporary file. Throws IOError — The directory does not exist, or the directory could not be deleted. On Windows, you cannot delete a directory that contains a file that is open.

SecurityError — The application does not have the necessary permissions to delete the directory. More examples Deleting directory contents. The following code creates an empty directory and then uses the deleteDirectory method to delete the directory. Events complete : Event — Dispatched when the directory has been deleted successfully.

Throws SecurityError — The application does not have the necessary permissions to delete the directory. Throws IOError — The file does not exist or could not to be deleted. On Windows, you cannot delete a file that is currently open. SecurityError — The application does not have the necessary permissions to delete the file.

More examples Deleting a file. The following code creates a temporary file and then calls the deleteFile method to delete it. Events complete : Event — Dispatched when the file has been deleted successfully. Throws SecurityError — The application does not have the necessary permissions to delete the file. Returns Array — An array of File objects. More examples Working with packages and symbolic links Enumerating directories. The following code shows how to use the getDirectoryListing method to enumerate the contents of the user directory.

Events ioError : ErrorEvent — You do not have adequate permissions to read this directory, or the directory does not exist. The contents event includes a files property, which is the resulting array of File objects. The following code shows how to use the getDirectoryListingAsync method to enumerate the contents of the user directory. Returns String — The relative path between this file or directory and the ref file or directory , if possible; otherwise null.

Throws ArgumentError — The reference is null. More examples Finding the relative path between two files. Returns Array — An array of File objects, listing the root directories. Learn more Pointing to the file system root. The following code outputs a list of root directories: Copy. The move process creates any required parent directories if possible. Throws IOError — The source does not exist; or the destination exists and overwrite is set to false ; or the source file or directory could not be moved to the target location; or the source and destination refer to the same file or folder and overwrite is set to true.

On Windows, you cannot move a file that is open or a directory that contains a file that is open. SecurityError — The application does not have the necessary permissions to move the file.

The following code shows how to use the moveTo method to rename a file. The original filename is test1. Since both the source and destination File object point to the same directory the Apollo Test subdirectory of the user's documents directory , the moveTo method renames the file, rather than moving it to a new directory. The following code shows how to use the moveTo method to move a file.

The original file is the test1. Events complete : Event — Dispatched when the file or directory has been successfully moved. Throws SecurityError — The application does not have the necessary permissions to move the file.

The following code shows how to use the moveToAsync method to rename a file. Since both the source and destination File object point to the same directory the Apollo Test subdirectory of the user's documents directory , the moveToAsync method renames the file, rather than moving it to a new directory. When you set overwrite parameter to true , the operation overwrites any existing test2. Throws IOError — The operating system did not allow the operation; or the file or directory does not exist.

On Windows, you cannot move a file that is open or a directory that contains a file that is currently open. SecurityError — The application does not have the necessary permissions to move the file to the trash.

More examples Moving a file to the trash. Events ioError : IOErrorEvent — The operating system did not allow the operation; or the file or directory does not exist. Throws SecurityError — The application does not have the necessary permissions to move the file to the trash.

You cannot open documents from the application directory. On Windows, attempting to open a file that has no associated application fails silently, without an exception. IllegalOperationError — The file is in the application directory or it is of a prohibited file type. This error does not apply for AIR applications installed with a native application installer extended desktop profile applications.

IOError — The file does not exist or no application is registered to open the file. ReferenceError — The file does not exist. More examples Opening files with the default system application. The following code lets the user navigate to an MP3 file and open it in the default application for playing MP3 files.

All resulting paths are normalized as follows: Any ". Any ".. Filenames and directory names are case-sensitive on Linux. Returns File — A new File object pointing to the resulting path. More examples Navigating to relative paths Modifying File paths. Event Object Type: flash. Event property Event. The Event. This event has the following properties: Property Value bubbles false cancelable false ; there is no default behavior to cancel.

The FileListEvent. Defines the value of the type property of an ioError event object. PermissionEvent property PermissionEvent. Check the value of status property to determine whether the permission was granted or denied Related API Elements File. The SecurityErrorEvent. Property Value bubbles false cancelable false ; there is no default behavior to cancel. Language Reference only. Dispatched when a pending asynchronous operation is canceled.

Dispatched when an asynchronous operation is complete. Dispatched if a call to the upload or uploadUnencoded method attempts to access data over HTTP and Adobe AIR is able to detect and return the status code for the request. Dispatched when an upload fails and an HTTP status code is available to describe the failure. Dispatched when an error occurs during an asynchronous file operation. Dispatched when an upload or download operation starts.

Dispatched periodically during the file upload or download operation. Dispatched when an operation violates a security constraint. Dispatched after data is received from the server after a successful upload. ArgumentError — The syntax of the path is invalid.

You can specify the path by using either a URL or native path platform-specific notation. ArgumentError — The syntax of the path parameter is invalid. IOError — The source does not exist; or the destination exists and overwrite is false ; or the source could not be copied to the target; or the source and destination refer to the same file or folder and overwrite is set to true.

IOError — The directory did not exist and could not be created. File — A File object referencing the new temporary directory. File — A File object referencing the new temporary file. IOError — The directory does not exist, or the directory could not be deleted. IOError — The file does not exist or could not to be deleted.

String — The relative path between this file or directory and the ref file or directory , if possible; otherwise null. ArgumentError — The reference is null. Use to specify a path relative to the application store directory.

For each installed application, AIR defines a unique application store directory, which is a useful place to store data specific to that application.

For example, the following path points to a prefs. Certain operating systems, most notably iOS and Mac OS X, provide users the ability to automatically back up application files to a remote storage. In addition, on iOS there are restrictions on whether files can be backed up and also where files of different purposes can be stored.

For further information see the next sections. To specify that a file does not need to be backed up and iOS only can be deleted by the operating system if device storage space runs low, save the file in the cache directory File.

This is the preferred storage location on iOS and should be used for most files that can be regenerated or re-downloaded. To specify that a file does not need to be backed up, but should not be deleted by the operating system, save the file in one of the application library directories such as the application storage directory File.

This is required by Apple for content that can be regenerated or downloaded again, but which is required for proper functioning of your application during offline use. By default all files in the application library folders are backed up. On Mac OS X this is the application storage directory.

On iOS, this includes the application storage directory, the application directory, the desktop directory, documents directory, and user directory because those directories are mapped to application library folders on iOS. Consequently, any files in those directories are backed up to server storage by default.

If you are saving a file in one of those locations that can be re-created by your application, you should flag the file so the operating system knows not to back it up.

On iOS, files in the application library folders such as the application storage directory or the documents directory are flagged as permanent and are not deleted by the operating system. Save files that can be regenerated by the application and are safe to delete in case of low storage space in the application cache directory. You access the cache directory using the File. On other operating systems, this directory is mapped to a comparable directory.

On Windows, the cache directory maps to the operating system temp directory. You can use the getRelativePath method to find the relative path between two files:. The second parameter of the getRelativePath method, the useDotDot parameter, allows for.. File and path names are not case sensitive on Windows and Mac OS. In the following, two File objects point to the same file:.

However, documents and directory names do include capitalization. For example, the following assumes that there is a folder named AIR Test in the documents directory, as in the following examples:. The canonicalize method converts the nativePath object to use the correct capitalization for the file or directory name. On case sensitive file systems such as Linux , when multiple files exists with names differing only in case, the canonicalize method adjusts the path to match the first file found in an order determined by the file system.

You can also use the canonicalize method to convert short file names "8. Symbolic links allow a file to point to another file or directory on disk. Although similar, symbolic links are not the same as aliases. An alias is always reported as a file rather than a directory , and reading or writing to an alias or shortcut never affects the original file or directory that it points to.

On the other hand, a symbolic link behaves exactly like the file or directory it points to. It can be reported as a file or a directory, and reading or writing to a symbolic link affects the file or directory that it points to, not the symbolic link itself. The File class includes the isPackage and isSymbolicLink properties for checking if a File object references a package or symbolic link.

The canonicalize method changes the path of a symbolic link to point to the file or directory to which the link refers. The spaceAvailable property of a File object is the space available for use at the File location, in bytes.

For example, the following code checks the space available in the application storage directory:. If the File object references a directory, the spaceAvailable property indicates the space in the directory that files can use.

If the File object references a file, the spaceAvailable property indicates the space into which the file could grow. If the file location does not exist, the spaceAvailable property is set to 0.

If the File object references a symbolic link, the spaceAvailable property is set to space available at the location the symbolic link points to. Typically the space available for a directory or file is the same as the space available on the volume containing the directory or file. However, space available can take into account quotas and per-directory limits. Adding a file or directory to a volume generally requires more space than the actual size of the file or the size of the contents of the directory.

For example, the operating system may require more space to store index information. Or the disk sectors required may use additional space. Also, available space changes dynamically. So, you cannot expect to allocate all of the reported space for file storage. For information on writing to the file system, see Reading and writing files.

In AIR 2, you can open a file using the application registered by the operating system to open it. Use the openWithDefaultApplication method of a File object to open the file. For example, the following code opens a file named test. The following code lets the user navigate to an mp3 file and open it in the default application for playing mp3 files:.

You cannot use the openWithDefaultApplication method with files located in the application directory. Attempting to open one of these files using the openWithDefaultApplication method results in an exception. For a complete list of prevented filetypes, see the language reference entry for the File.

Adobe Flash Platform. A File object is a pointer to a file or directory in the file system. About the File class You can use the File class for the following: Getting the path to special directories, including the user directory, the user's documents directory, the directory from which the application was launched, and the application directory Coping files and directories Moving files and directories Deleting files and directories or moving them to the trash Listing files and directories contained in a directory Creating temporary files and folders Once a File object points to a file path, you can use it to read and write file data, using the FileStream class.

Paths of File objects Each File object has two properties that each define its path: Property Description nativePath Specifies the platform-specific path to a file. Note: When a platform does not define standard locations for desktop, documents, or user directories, File.

Pointing a File object to a directory There are different ways to set a File object to point to a directory. Pointing to the desktop directory You can point a File object to the desktop. Pointing to the application storage directory You can point a File object to the application storage directory. Pointing to the application directory You can point a File object to the directory in which the application was installed, known as the application directory.

The nativePath property is an empty string.



0コメント

  • 1000 / 1000