解析大文本文件的Adobe AIR [英] Parsing large text files with Adobe AIR

查看:132
本文介绍了解析大文本文件的Adobe AIR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试做以下空气中:

  1. 在浏览到一个文本文件
  2. 阅读文本文件,并将其存储在一个字符串(最终在一个数组)
  3. 分割字符串的分隔符\ n和把生成的字符串数组中的
  4. 将其发送到一个网站之前,操作这些数据(mysql数据库)

文本文件,我处理的将是100-500mb大小的任何地方。到目前为止,我已经能够完成步骤1和2,这里是我的code:

The text files I am dealing with will be anywhere from 100-500mb in size. So far, I've been able to to complete steps 1 and 2, here is my code:

<mx:Script>
	<![CDATA[
	import mx.collections.ArrayCollection;
	import flash.filesystem.*;
	import flash.events.*;
	import mx.controls.*;

	private var fileOpened:File = File.desktopDirectory;
	private var fileContents:String;
	private var stream:FileStream;

	private function selectFile(root:File):void {
		var filter:FileFilter = new FileFilter("Text", "*.txt");
		root.browseForOpen("Open", [filter]);
		root.addEventListener(Event.SELECT, fileSelected);
	}

	private function fileSelected(e:Event):void {
		var path:String = fileOpened.nativePath;
		filePath.text = path;

		stream = new FileStream();
		stream.addEventListener(ProgressEvent.PROGRESS, fileProgress);
		stream.addEventListener(Event.COMPLETE, fileComplete);
		stream.openAsync(fileOpened, FileMode.READ);
	}

	private function fileProgress(p_evt:ProgressEvent):void {
		fileContents += stream.readMultiByte(stream.bytesAvailable, File.systemCharset); 
		readProgress.text = ((p_evt.bytesLoaded/1048576).toFixed(2)) + "MB out of " + ((p_evt.bytesTotal/1048576).toFixed(2)) + "MB read";
	}

	private function fileComplete(p_evt:Event):void {
		stream.close();
		//fileText.text = fileContents;
	}

	private function process(c:String):void {
		if(!c.length > 0) {
			Alert.show("File contents empty!", "Error");
		}
		//var array:Array = c.split(/\n/);

	}

	]]>
</mx:Script>

下面是MXML

<mx:Text x="10" y="10" id="filePath" text="Select a file..." width="678" height="22" color="#FFFFFF"  fontWeight="bold"/>
<mx:Button x="10" y="40" label="Browse" click="selectFile(fileOpened)" color="#FFFFFF" fontWeight="bold" fillAlphas="[1.0, 1.0]" fillColors="[#E2E2E2, #484848]"/>
<mx:Button x="86" y="40" label="Process" click="process(fileContents)" color="#FFFFFF" fontWeight="bold"  fillAlphas="[1.0, 1.0]" fillColors="[#E2E2E2, #484848]"/>
<mx:TextArea x="10" y="70" id="fileText" width="678" height="333" editable="false"/>
<mx:Label x="10" y="411" id="readProgress" text="" width="678" height="19" color="#FFFFFF"/>

第三步是在那里我遇到了一些麻烦。有2条线在我的code注释掉,这两条线会导致程序冻结。

step 3 is where I am having some troubles. There are 2 lines in my code commented out, both lines cause the program to freeze.

fileText.text = fileContents;尝试把字符串的内容在一个textarea
VAR数组:数组= c.split(/ \ N /);试图拆分字符串通过分隔符换行符

fileText.text = fileContents; attempts to put the contents of the string in a textarea
var array:Array = c.split(/\n/); attempts to split the string by delimiter newline

可以使用一些投入在这一点上... 上午我甚至会对此正确的方法吗? 可挠曲/空气处理文件这么大? (我认为如此) 这是我在做任何形式的弹性工作第一次尝试,如果你看到其他的东西香港专业教育学院做了错事或者可以做的更好,我想AP preciate头了!

Could use some input at this point... Am i even going about this the right way? Can flex/air handle files this large? (i'd assume so) This is my first attempt at doing any sort of flex work, if you see other things ive done wrong or could be done better, i'd appreciate the heads up!

谢谢!

推荐答案

在一个500MB的文件做一个分割可能不是一个好主意。您可以编写自己的解析器来工作的文件,但它可能不会很快之一:

Doing a split on a 500MB file might not be a good idea. You can write your own parser to work on the file but it may not be very fast either:

private function fileComplete(p_evt:Event):void 
{
    var array:Array = [];

    var char:String;
    var line:String = "";
    while(stream.position < stream.bytesAvailable)
    {
        char = stream.readUTFBytes(1);
        if(char == "\n")
        {
            array.push(line);
            line = "";
        }
        else
        {
            line += char;
        }
    }

    // catch the last line if the file isn't terminated by a \n
    if(line != "")
    {
        array.push(line);
    }

    stream.close();
}

我没有测试它,但它应通过文件字符只是一步一个字符。如果该字符是一个新行,然后推旧线到阵列否则它添加到当前行。

I haven't tested it but it should just step through the file character by character. If the character is a new line then push the old line into the array otherwise add it to the current line.

如果你不希望它阻止你的用户界面,而你做到这一点,你需要抽象成一个基于定时器的想法:

If you don't want it to block your UI while you do it, you'll need to abstract it into a timer based idea:

// pseudo code
private function fileComplete(p_evt:Event):void 
{
    var array:Array = [];
    processFileChunk();
}

private function processFileChunk(event:TimerEvent=null):void
{
    var MAX_PER_FRAME:int = 1024;
    var bytesThisFrame:int = 0;
    var char:String;
    var line:String = "";
    while(   (stream.position < stream.bytesAvailable)
          && (bytesThisFrame < MAX_PER_FRAME))
    {
        char = stream.readUTFBytes(1);
        if(char == "\n")
        {
            array.push(line);
            line = "";
        }
        else
        {
            line += char;
        }
        bytesThisFrame++;
    }

    // if we aren't done
    if(stream.position < stream.bytesAvailable)
    {
        // declare this in the class
        timer = new Timer(100, 1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, processFileChunk);
        timer.start();
    }
    // we're done
    else
    {
        // catch the last line if the file isn't terminated by a \n
        if(line != "")
        {
            array.push(line);
        }

        stream.close();

        // maybe dispatchEvent(new Event(Event.COMPLETE)); here
        // or call an internal function to deal with the complete array
    }
}

基本上你选择,处理每个帧(MAX_PER_FRAME),然后过程很多字节的文件的量。如果你去的字节数则只是做一个计时器再次调用处理功能在几帧时间,它应该继续它停止的位置。一旦你确定你是完整的,你可以指派调用另一个函数的事件。

Basically you choose an amount of the file to process each frame (MAX_PER_FRAME) and then process that many bytes. If you go over the number of bytes then just make a timer to call the process function again in a few frames time and it should continue where it left off. You can dispatch an event of call another function once you are sure you are complete.

这篇关于解析大文本文件的Adobe AIR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆