Text Files

For the first example, we will read a file that contains a list of names.

There is nothing in the movie except the ActionScript:

1
2
3
4
5
6
7
8
9
var file:URLRequest = new URLRequest("http://www.zebra0.com/resources/names.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 {
    // The output of the text file is available via the data property of URLLoader.
    trace(loader.data);
}
CODE

When you run the program you will see the names in the output window.

Explanation:

  • 1. The URLRequest tells ActionScript where to find the file.
  • 2. and 3. The loader actually reads the files and stores the result in the loaders data.
  • 5. We use a listener to let us know when the files has completely loaded.
  • 6-9. When the file has finished loading we display the contents of loader.data using trace.

NEXT: Read names and store in Array