Friday, March 18, 2011

Files and directories in AIR: Part 8

Today we will learn how to read some information off a file. We will create a simple application to read text files and display the contents of a text document.

Firstly, create a text document that we will later read. Remember to put the encoding to UTF (or Unicode), although this is not completely required, but very recommended, since it supports all characters.

Now, create a new AIR document and make a button and a text field, put them on stage. Give them instance names of myButton and myText. Make the text field large, make sure it is dynamic, allows multiple lines and has all the necessary symbols embedded.

Next - the coding.

Firstly add a listener for the button.

myButton.addEventListener(MouseEvent.CLICK, onBrowse);

The button will be used to call out a browser window to select a text file from the user's computer.

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

After that, we can make the function that is called out when the user selects a file and hits OK. Here we will need to set the text field empty and create a new FileStream object, add listeners for it and use a method called openAsync, to which we provide the reference to our file and the file mode. We need to cast our evt.target (which is our file object) to be read as a File object.

function onSelected(evt:Event):void
{
myText.text = "";
var myStream:FileStream = new FileStream();
myStream.addEventListener(ProgressEvent.PROGRESS, onProgress);
myStream.addEventListener(Event.COMPLETE, onComplete);
myStream.openAsync((evt.target as File), FileMode.READ);
}

When we're tracking the progress, we want to write down the info we receive. Use the method readUTFBytes for the FileStream object to write down the data.

function onProgress(evt:ProgressEvent):void
{
if (evt.target.bytesAvailable)
{
myText.appendText(evt.target.readUTFBytes(evt.target.bytesAvailable));
}
}

When the whole process is complete, we need to close our stream.

function onComplete(evt:Event):void
{
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;
import flash.events.ProgressEvent;

myButton.addEventListener(MouseEvent.CLICK, onBrowse);

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

function onSelected(evt:Event):void
{
myText.text = "";
var myStream:FileStream = new FileStream();
myStream.addEventListener(ProgressEvent.PROGRESS, onProgress);
myStream.addEventListener(Event.COMPLETE, onComplete);
myStream.openAsync((evt.target as File), FileMode.READ);
}

function onProgress(evt:ProgressEvent):void
{
if (evt.target.bytesAvailable)
{
myText.appendText(evt.target.readUTFBytes(evt.target.bytesAvailable));
}
}

function onComplete(evt:Event):void
{
evt.target.close();
}

That's all for now. 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 9
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