« Write your first file to the file system using Apollo | Main | Create your first Apollo Window »
File and FileStream within Apollo - full source code
| By Rich Tretola | March 19, 2007 | Print This Post
|
| 1,129 views |

Local access to the file system is what separates Apollo from Flex/Flash. This simple sample demonstrates how to read and write files to the local file system.
Here is the function that loads a file:
- private function loadFile():void{
- var file:File = File.appStorageDirectory;
- file = file.resolve("Files/" + fdg.selectedItem.name);
- stream = new FileStream();
- stream.open(file, FileMode.READ);
- var str:String = stream.readUTFBytes(stream.bytesAvailable);
- stream.close();
- str = str.replace(File.lineEnding, "\n");
- contents.text = str;
- fileName.text = file.name;
- }
Here is the function that saves the file:
- private function saveFile():void{
- var myPattern:RegExp = / /g;
- var newFileName:String = fileName.text.replace('.txt','');
- if(newFileName.length > 1){
- var file:File = File.appStorageDirectory.resolve("Files/" + newFileName.replace(myPattern,'_') + ".txt");
- var stream:FileStream = new FileStream()
- stream.open(file, FileMode.WRITE);
- var str:String = contents.text;
- str = str.replace(/\r/g, File.lineEnding);
- stream.writeUTFBytes(str);
- stream.close();
- fdg.directory = File.appStorageDirectory.resolve("Files/");
- fileName.text = "";
- contents.text = "";
- mx.controls.Alert.show("File has been saved", "Notice");
- } else {
- mx.controls.Alert.show("File name is required", "Error Saving File");
- }
- }
Note that although this application uses File.appStorageDirectory, you also have easy access within Apollo to:
File.appResourceDirectory
File.appStorageDirectory
File.currentDirectory
File.desktopDirectory
File.documentsDirectory
Topics: Adobe AIR, Tutorials |









March 26th, 2007 at 10:57 pm
what is the purpose of this line:
fdg.directory = File.appStorageDirectory.resolve(”Files/”)
in both functions?
March 27th, 2007 at 3:25 am
Well, it is the way to get the path to a file. The two functions are looking at the same folder but different files. The loadFile function gets a reference to the selected filename within the Files folder.
var file:File = File.appStorageDirectory;
file = file.resolve(”Files/” + fdg.selectedItem.name);
The saveFile function gets a refernce to the new filename within the Files folder.
var file:File = File.appStorageDirectory.resolve(”Files/” + newFileName.replace(myPattern,’_’) + “.txt”);
June 20th, 2007 at 6:16 pm
The code works but I had to change the
“appStorageDirectory” to:”applicationStorageDirectory”
As well as the WindowedApplication used by Flex3.
I’m getting an error when I load “Windows - No Disk” Exception Processing Message c00000013 Parameters 75b6bf9c 4 75b6…. [cancel] [Try Again] [Continue]. If you continue the program works. I commented out the init() function to see if that would help but I am still getting the error. I hope this helps.
June 20th, 2007 at 6:54 pm
This application was built for the Apollo alpha and does not play nicely with the AIR beta and many things have changed between builds.
November 23rd, 2007 at 1:23 pm
Was the No Disk error ever solved? I am getting the same thing in my registered copy of FlexBuilder 2.