views:

3513

answers:

8

Hi,

I need code is as3 to reading a text file line by line and insert in to an array using AS3. is it possible to do it with out having any special character.

sample.txt

    Car
    van
    scooter
    bike

I need to read the file and insert in to array like...Array[0]=car,Array[1]=car.....

Thanks In advance

+4  A: 

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
thank u lot Mr.kekoav... its woks fine
vineth
A: 

It 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
A: 

I just wonder why text? Use XML which is easy-to-parse and easy-to-support...

Loading is OK in kekoav's and in Sefler's examples.

Jet
A: 

Just telling you, the "1=Car 2=van 3=scooter 4=bike" solution is a very ugly solution and would make your project fail in case one of the items contains numeric information (e.g. Peugeot 303). Personally I wouldn't even bother looking for a regular expression to workaround this (sorry to be pessimistic, you may have very good reasons not being able to change the way this file is output).

Theo.T
hi,The reason why iam maintaining in this way is i have 100 different name in the text file ,if we need to edit any values means we can use the number as key for that value(vehicle name).. 1=Car 2=van 3=scooter 4=bike
vineth
+1  A: 

hmmm ... it's really a bit strange to use space as a seperator ... 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?

greetz back2dos

back2dos
A: 

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
A: 

Very interesting pieces of code -- thanks for that -- my Solarlog appliance saves in .js format -- this will probably get me the from that log perfectly. Although mine does rely on the newline char.. not on the " " -- will char 13 do the job ? or is there another way in Actionscript ?

Binabik

binabik
A: 

lol.. just noticed.. var myArrayOfLines:Array = e.target.data.split(/\n/)

thanks a bunch folks.

Binabik

binabik