Saturday, March 19, 2011

Files and directories in AIR: Part 9

This tutorial will be the first part of creating a simple application to write and save files that store information as Objects.

First of all, let's create the object itself.

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

When we trace it, we get this output: hi,hello,greetings.

Now, create two buttons on stage. Call them mySaveButton and myLoadButton. These will be used to open browser windows for saving and loading files from the user's hard drive.

In this tutorial we will set up the saving part of the application.

Create a listener for the save button:

mySaveButton.addEventListener(MouseEvent.CLICK, onBrowseSave);

When the button is clicked, a new File object is created with a link to the desktop directory. A browser window opens to select a place to save the file to.

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

When the user selects the directory to save the file to, we create a new FileStream object and start writing data to it. We use the writeObject() method to pass an object to 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);
}

When the save completes, we can get a notification.

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

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();
}

We've only created the first part of the application - only the saving part. We'll do the rest in the next tutorial.

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 10
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