10

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
Jordan.J.D
  • 7,999
  • 11
  • 48
  • 78
vineth
  • 873
  • 5
  • 33
  • 57

6 Answers6

27

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.

Kekoa
  • 27,892
  • 14
  • 72
  • 91
7

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.

Sefler
  • 2,237
  • 5
  • 19
  • 29
  • 1
    yes, but you cannot read a 700 MBytes large file into the memory! With fileStream.readUTFBytes(fileStream.bytesAvailable); you get the contents of the file, but if it's too large, you application is dead :) I have no idea how to read a file line by line neither, that's why I'm here.. – Janov Byrnisson Nov 17 '10 at 12:59
  • also, as of some more recent version (not sure which) they have modified `resolve` to `resolvePath`. – Yevgeny Simkin Aug 06 '12 at 20:26
1

This will work for small files, not for humongous files like say firewall or webserver log files.

Those files won't load completely into memory at all.

Is there a solution to 1/ read a file until you encounter the end of line char, 2/ process that which is read, 3/ and then continue until the next end of line/end of file char ?

I think it would be possible using filestream reading 1 byte at a time and seeing if it contains a newline char, and pushing that byte content onto an array until you encounter the EOL char, but I'm not (yet) strong enough in AS to write something like that without loosing 3 months of my life... plus I'm not too sure about the speed of the process.

Anyone ? I would prefer to not launch a seperate question as this essentially the same demand but for larger files...

Alex Boschmans
  • 515
  • 2
  • 12
  • good question, I think it should have it's own topic, have you found a solution if yes please share it. Thx – simion314 Jun 07 '13 at 07:31
  • No sorry, I actually stopped using flex - I'm used to Python where I could do exactly the above using only a few lines of code. With flex I kept bumping into gotchas and extra work, so in the end I went back to using python. – Alex Boschmans Jun 12 '13 at 19:33
1

hmmm, it's really a bit strange to use space as a separator. I mean, you could do it this way:

var result:Array = [];
for each (var s:String in source.split(" ")) {
     var a:Array = s.split("=");
     result[a[0]] = a[1];
}

yet relying on " " for splitting, really is not such a good idea, can't you use JSON, CSV or XML?

Taryn
  • 242,637
  • 56
  • 362
  • 405
back2dos
  • 15,588
  • 34
  • 50
0

For those wondering about large text files, you can't know when there'll be a line break before you read the line break byte. What I suggest is to have a loop that would instead of read all bytes with

fileStream.readUTFBytes(fileStream.bytesAvailable);

You do a few hundred bytes at a time, process whatever you need, then continue reading after.

Thiago Camargo
  • 148
  • 1
  • 6
0

if you are using a real time recording, text is more fast to generate than others like .json or .xml. But xml and json are more easy to read and parse.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202