Flex 4 (Adobe Air)如何在应用程序目录(ApplicationDirectory)下写入文件

要在Flex中读写文件,离不开两个类:flash.filesystem.File和flash.filesystem.FileStream。File代表着一个文件或者文件夹的路径,而FileStream指文件流。

File类中定义了几个常量,用以指向一些常用的目录:

The File class includes static properties that let you reference commonly used directory locations. These static properties include:

  • File.applicationStorageDirectory—a storage directory unique to each installed AIR application
  • File.applicationDirectory—the read-only directory where the application is installed (along with any installed assets)
  • File.desktopDirectory—the user’s desktop directory
  • File.documentsDirectory—the user’s documents directory
  • File.userDirectory—the user directory
    其中File.applicationDirectory便是本文所提到的应用程序目录。很遗憾,这是只读的。所以,如果你用下面的方式(示例代码1)去往这个目录写文件的话,运行时是会有异常的。

var file:File = File.applicationDirectory.resolvePath(“test.txt”);
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE); // 此行运行时会有异常

查看FileStream类的open方法,可知,该方法运行时会抛出两种异常,如下:

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.
所以,如果执行“示例代码1”,便会有SecurityError异常发生。

需要注意到就是File.applicationDirectory虽然代表是应用程序的安装路径,但它仍是一个File对象而不是代表那个路径的字符串。这就带来了思路:也就是说,应用程序目录只读也只是针对File.applicationDirectory这个对象或者经由这个对象调用resolvePath这个方法所得到的相对路径下的文件只读。那么,如果我拿到了应用程序目录的绝对路径,然后自己new一个File对象,那么,是否还是是只读呢?

查看File类的API文档可知,File类有一个属性叫做nativePath:

The full path in the host operating system representation. On Mac OS and Linux, the forward slash (/) character is used as the path separator. However, in Windows, you can set the nativePath property by using the forward slash character or the backslash () character as the path separator, and AIR automatically replaces forward slashes with the appropriate backslash character.

Before writing code to set the nativePath property directly, consider whether doing so may result in platform-specific code. For example, a native path such as “C:\Documents and Settings\bob\Desktop” is only valid on Windows. It is far better to use the following static properties, which represent commonly used directories, and which are valid on all platforms:

File.applicationDirectory

File.applicationStorageDirectory

File.desktopDirectory

File.documentsDirectory

File.userDirectory

You can use the resolvePath() method to get a path relative to these directories.
所以,请看示例代码2:

var file:File = File.applicationDirectory.resolvePath(“test.txt”);
var fileStream:FileStream = new FileStream();
fileStream.open(new File(file.nativePath), FileMode.WRITE);

运行示例代码2,一切正常。

参考链接1:http://blog.teemusk.com/2011/10/writing-to-applicationdirectory-with-adobe-air/

参考链接2:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html

参考链接3:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html#open()