Calendar

March 2007
M T W T F S S
« Feb   Apr »
 1234
567891011
12131415161718
19202122232425
262728293031  

Tag Cloud

Categories

Archives

Highest Rated

Most Viewed

Recent Posts

Recent Comments


« 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, 2007Print This Post Print This Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
1,129 views

filereadwrite.jpg

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:

  1. private function loadFile():void{
  2. var file:File = File.appStorageDirectory;
  3. file = file.resolve("Files/" + fdg.selectedItem.name);
  4. stream = new FileStream();
  5. stream.open(file, FileMode.READ);
  6. var str:String = stream.readUTFBytes(stream.bytesAvailable);
  7. stream.close();
  8. str = str.replace(File.lineEnding, "\n");
  9. contents.text = str;
  10. fileName.text = file.name;
  11. }

Here is the function that saves the file:

  1. private function saveFile():void{
  2. var myPattern:RegExp = / /g;
  3. var newFileName:String = fileName.text.replace('.txt','');
  4. if(newFileName.length > 1){
  5. var file:File = File.appStorageDirectory.resolve("Files/" + newFileName.replace(myPattern,'_') + ".txt");
  6. var stream:FileStream = new FileStream()
  7. stream.open(file, FileMode.WRITE);
  8. var str:String = contents.text;
  9. str = str.replace(/\r/g, File.lineEnding);
  10. stream.writeUTFBytes(str);
  11. stream.close();
  12. fdg.directory = File.appStorageDirectory.resolve("Files/");
  13. fileName.text = "";
  14. contents.text = "";
  15. mx.controls.Alert.show("File has been saved", "Notice");
  16. } else {
  17. mx.controls.Alert.show("File name is required", "Error Saving File");
  18. }
  19. }

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

Download the application

Download source code

Share/Save/Bookmark

Topics: Adobe AIR, Tutorials |

5 Responses to “File and FileStream within Apollo - full source code”

  1. Critter Says:
    March 26th, 2007 at 10:57 pm

    what is the purpose of this line:
    fdg.directory = File.appStorageDirectory.resolve(”Files/”)

    in both functions?

  2. everythingflex Says:
    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”);

  3. MatteoNY Says:
    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.

  4. everythingflex Says:
    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.

  5. Nancy Gill Says:
    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.

Comments