Calendar

February 2008
M T W T F S S
« Jan   Mar »
 123
45678910
11121314151617
18192021222324
2526272829  

Tag Cloud

Categories

Archives

Highest Rated

Most Viewed

Recent Posts

Recent Comments


« AIR FileSystem Components | Main | Create Images with AIR and JPEGEncoder »

File and FileStream within AIR

By Rich Tretola | February 25, 2008Print This Post Print This Post
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.5 out of 5)
Loading ... Loading ...
1,326 views

filestream.jpg

Local access to the file system is what separates AIR from browser based 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.desktopDirectory.resolvePath("Files/" + fdg.selectedItem.name);;
  3.         stream = new FileStream();
  4.         stream.open(file, FileMode.READ);
  5.         var str:String = stream.readUTFBytes(stream.bytesAvailable);
  6.         stream.close();
  7.         str = str.replace(File.lineEnding, "\n");
  8.         contents.text = str;
  9.         fileName.text = file.name;
  10.     }

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.desktopDirectory.resolvePath("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.desktopDirectory.resolvePath("Files/");
  13.             fileName.text = "";
  14.             contents.text = "";
  15.           } else {
  16.               mx.controls.Alert.show("File name is required", "Error Saving File");
  17.           }
  18.     }

Note that although this application uses File.desktopDirectory, you also have easy access within Apollo to:

File.applicationDirectory
File.applicationStorageDirectory
File.desktopDirectory
File.documentsDirectory
File.userDirectory

Here is the full source:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
  3.     layout="absolute" creationComplete="init()" width="808" height="484"
  4.     backgroundGradientColors="[#FFFFFF, #5df098]">
  5.  
  6.     <mx:Script>
  7.     <![CDATA[
  8.  
  9.     import flash.filesystem.*;
  10.  
  11.     private var stream:FileStream;
  12.  
  13.     private function init():void{
  14.         var file:File = File.desktopDirectory.resolvePath("Files/sample.txt");
  15.         var stream:FileStream = new FileStream()
  16.         stream.open(file, FileMode.WRITE);
  17.         var str:String = "This is a sample file";
  18.         stream.writeUTFBytes(str);
  19.         stream.close();
  20.         fdg.directory = File.desktopDirectory.resolvePath("Files/");
  21.     }
  22.  
  23.     private function saveFile():void{
  24.         var myPattern:RegExp = / /g;
  25.         var newFileName:String = fileName.text.replace('.txt','');
  26.         if(newFileName.length > 1){
  27.             var file:File = File.desktopDirectory.resolvePath("Files/" + newFileName.replace(myPattern,'_') + ".txt");
  28.             var stream:FileStream = new FileStream()
  29.             stream.open(file, FileMode.WRITE);
  30.             var str:String = contents.text;
  31.             str = str.replace(/\r/g, File.lineEnding);
  32.             stream.writeUTFBytes(str);
  33.             stream.close();
  34.             fdg.directory = File.desktopDirectory.resolvePath("Files/");
  35.             fileName.text = "";
  36.             contents.text = "";
  37.           } else {
  38.               mx.controls.Alert.show("File name is required", "Error Saving File");
  39.           }
  40.     }
  41.  
  42.     private function loadFile():void{
  43.         var file:File = File.desktopDirectory.resolvePath("Files/" + fdg.selectedItem.name);;
  44.         stream = new FileStream();
  45.         stream.open(file, FileMode.READ);
  46.         var str:String = stream.readUTFBytes(stream.bytesAvailable);
  47.         stream.close();
  48.         str = str.replace(File.lineEnding, "\n");
  49.         contents.text = str;
  50.         fileName.text = file.name;
  51.     }
  52.  
  53.     ]]>
  54.     </mx:Script>
  55.     <mx:FileSystemDataGrid x="93" y="26" width="700" height="225" id="fdg" change="loadFile()"/>
  56.     <mx:Label text="File name:" x="18" y="261" fontWeight="bold"/>
  57.     <mx:Label x="19" y="301" text="Contents:" fontWeight="bold"/>
  58.     <mx:TextInput x="89" y="259" id="fileName"/>
  59.     <mx:TextArea x="89" y="300" id="contents" width="629" height="156"/>
  60.     <mx:Label x="10" y="27" text="Current Files" fontWeight="bold"/>
  61.     <mx:Button x="726" y="432" label="Save" click="saveFile()" icon="@Embed('assets/images/Save_icon.gif')"/>
  62.  
  63. </mx:WindowedApplication>

Share/Save/Bookmark

Topics: Adobe AIR |

8 Responses to “File and FileStream within AIR”

  1. Daniel Says:
    June 7th, 2008 at 12:09 am

    Can you save a of stream of a file located in a website? I want to build a multi-file downloader manager. Thanks

  2. everythingflex Says:
    June 7th, 2008 at 8:10 am

    I am not sure what you are asking? The FileReference class already supports multi file downloads.

  3. bas Says:
    June 18th, 2008 at 4:41 am

    1. I want to create a file and write the data into it in flex application.
    can we do this without using AIR. ?

    2. I have tried the source code which you have pasted above getting error as “Unable to locate specified base class mx.core.WindowedApplication ”
    What should be the fix and please let me know how can i run the above ex.

    Thanks
    mba_soft@rediff.com

  4. everythingflex Says:
    June 18th, 2008 at 7:19 am

    1. No, the flash player security of the flash player running in the browser does not allow you to access the users file system. This can only be done via AIR.

    2. WindowedApplication is the root tag of AIR applications and not part of the Flex Framework, so it would not be found if you created your project as a Flex Web Application.

  5. bas Says:
    June 19th, 2008 at 2:35 am

    Thx ..

    infact my application already created as a Flex Web application.
    1. should I recreate my project application type as AIR to work on file system?
    2. cant i integrate AIR in my existing developed Flex Web app. ?

  6. everythingflex Says:
    June 19th, 2008 at 5:02 am

    1. Yes, you would need to be working in an AIR application to interact with the file system.
    2. I am not sure what you mean? What are you trying to do?

  7. bas Says:
    June 19th, 2008 at 7:16 am

    My application already built in flex framework using cairngorm.

    In which we came a requirement like create a file, write the data into it and store it as (.vcal extension)vcal format on user system.

    please suggest us as how can we develop this in our flex application?

  8. everythingflex Says:
    June 19th, 2008 at 7:51 am

    You can compile it as an AIR application by simply changing the root tag from mx:Application to mx:WindowedApplication

    If you want the application to be a Flex web based app, you would need to create the .vcal file on the server using server side technology (ColdFusion, php, java, etc) and then allow the user to download the file using the FileReference class.

Comments