Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading the text file line by line and push to an array in AS3

I need some code in AS3 that will read a text file line by line and insert it into an array. Is this possible without having any special character?

sample.txt

    car
    van
    scooter
    bike

I need to read the file and insert it into an array like:

Array[0]=car
Array[1]=van
Array[2]=scooter
Array[3]=bike
like image 730
vineth Avatar asked May 20 '09 13:05

vineth


2 Answers

Something like this may work:

var myTextLoader:URLLoader = new URLLoader();

myTextLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e:Event):void {
    var myArrayOfLines:Array = e.target.data.split(/\n/);
}

myTextLoader.load(new URLRequest("myText.txt"));

The array in the onLoaded function will have your array of items.

Edit- for fun, I ran the code with a sample file and it worked like a charm.

like image 118
Kekoa Avatar answered Nov 03 '22 10:11

Kekoa


Here is an example of loading and reading different file types with ActionScript 3:

      import flash.filesystem.FileMode;
      import flash.filesystem.FileStream;
      import flash.filesystem.File;

      var myFile:File = File.appResourceDirectory; // Create out file object and tell our File Object where to look for the file
      myFile = myFile.resolve("mySampleFile.txt"); // Point it to an actual file

      var fileStream:FileStream = new FileStream(); // Create our file stream
      fileStream.open(myFile, FileMode.READ);

      var fileContents:String = fileStream.readUTFBytes(fileStream.bytesAvailable); // Read the contens of the 
      fileContents_txt.text = fileContents; // Display the contents. I've created a TextArea on the stage for display

      fileStream.close(); // Clean up and close the file stream

After reading the string, you can use the int.valueOf() to convert the string to the integer.

like image 27
Sefler Avatar answered Nov 03 '22 09:11

Sefler