Sunday, March 20, 2011

Files and directories in AIR: Part 10

In this tutorial we create the second part of our AIR application, that lets us save and read files with its data stored in an Object.

Now that we have the saving feature working, we need to make the loading part.

First make sure you have the myLoadButton button on the stage. Add a listener for it:

myLoadButton.addEventListener(MouseEvent.CLICK, onBrowseLoad);

When the load button is clicked, we want a browser window to pop up. To do that, we create a File object and use the browseForOpen() method.

function onBrowseLoad(evt:MouseEvent):void
{
 var myFile:File = File.desktopDirectory;
 myFile.addEventListener(Event.SELECT, onSelectedLoad);
 myFile.browseForOpen("Select a file");
}

When a file is selected and the user confirms it, we create a new FileStream object to read the file:

function onSelectedLoad(evt:Event):void
{
 var myStream:FileStream = new FileStream();
 myStream.addEventListener(Event.COMPLETE, onCompleteLoad);
 myStream.openAsync((evt.target as File), FileMode.READ);
}

When the loading is completed, we can do whatever we want with the object. Let's just trace it:

function onCompleteLoad(evt:Event):void
{
 trace(evt.target.readObject());
 evt.target.close();
}

If we test our movie now and open a file, the trace will give an output of the contents of the object in our generated file.

Here is the full code:

import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.events.MouseEvent;
import flash.events.Event;

var myObject:Object = ["hi", "hello", "greetings"];
trace(myObject);

mySaveButton.addEventListener(MouseEvent.CLICK, onBrowseSave);

function onBrowseSave(evt:MouseEvent):void
{
 var myFile:File = File.desktopDirectory;
 myFile.addEventListener(Event.SELECT, onSelectedSave);
 myFile.browseForSave("Save the file");
}

function onSelectedSave(evt:Event):void
{
 var myStream:FileStream = new FileStream();
 myStream.addEventListener(Event.COMPLETE, onCompleteSave);
 myStream.openAsync((evt.target as File), FileMode.WRITE);
 myStream.writeObject(myObject);
}


function onCompleteSave(evt:Event):void
{
 trace("File saved");
 evt.target.close();
}

myLoadButton.addEventListener(MouseEvent.CLICK, onBrowseLoad);

function onBrowseLoad(evt:MouseEvent):void
{
 var myFile:File = File.desktopDirectory;
 myFile.addEventListener(Event.SELECT, onSelectedLoad);
 myFile.browseForOpen("Select a file");
}

function onSelectedLoad(evt:Event):void
{
 var myStream:FileStream = new FileStream();
 myStream.addEventListener(Event.COMPLETE, onCompleteLoad);
 myStream.openAsync((evt.target as File), FileMode.READ);
}


function onCompleteLoad(evt:Event):void
{
 trace(evt.target.readObject());
 evt.target.close();
}

Using this technique, we can let the user create its own files to store information that can be later used by this or some other AIR applications, and not necessarily AIR, if you want to.

Thanks for reading!

Related:

Files and directories in AIR: Part 1
Files and directories in AIR: Part 2
Files and directories in AIR: Part 3
Files and directories in AIR: Part 4
Files and directories in AIR: Part 5
Files and directories in AIR: Part 6
Files and directories in AIR: Part 7
Files and directories in AIR: Part 8
Files and directories in AIR: Part 9
Files and directories in AIR: Part 11
Create a log application using XML and AIR: Part 1
Create a log application using XML and AIR: Part 2
Create a log application using XML and AIR: Part 3
Create a log application using XML and AIR: Part 4
Files and directories in AIR: Part 12

No comments:

Post a Comment