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
1

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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..
also, as of some more recent version (not sure which) they have modified resolve to resolvePath.
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...

2 Comments

good question, I think it should have it's own topic, have you found a solution if yes please share it. Thx
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.
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?

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.