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.tx
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.
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.
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.
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...
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.
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?