Text Files

Look at the combo links program.

Instead of embedding the list of websites inside the movie, we can read a list of websites from a file.

Amazon,http://www.google.com
Google,http://www.google.com
Hello-World,http://www.hello-world.com
Orioles,http://www.orioles.com
Yahoo,http://www.yahoo.com"
CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import fl.data.*;
var websites:Array =[];
cboLinks.addEventListener(Event.CHANGE, linktoPage);

var file:URLRequest = new URLRequest("http://www.zebra0.com/resources/favorites.txt");
var loader:URLLoader = new URLLoader(); //a loader for the file
loader.load(file);
// Listen for when the file has finished loading.
loader.addEventListener(Event.COMPLETE, loaderComplete);
function loaderComplete(e:Event):void {
  trace(loader.data);
  var names:Array = loader.data.split(/\n/);
  for(var i=0;i<names.length;i++) {
    var parts:Array=names[i].split(",");
    websites[i]={label:parts[0],data:parts[1]};
  }
  cboLinks.dataProvider = new DataProvider(websites);
}

function linktoPage(event:Event):void {
  var site:URLRequest = new URLRequest();
  site.url = cboLinks.selectedItem.data;
  navigateToURL(site,"_blank"); //_blank, _self, _parent
} //linktoPage
CODE

Explanation

  • 12: We break the file into lines, splitting on the line feed.
  • 13: For each line in the file we add to the array of websites.
  • 14: Each line is split on the comma.
  • 15: Each element of the array has a label and data.
  • 17: After creating the array, we assign it as the data provider for the combo box.

NEXT: XML