与AS3 txt文件工作 [英] Working with txt file on as3

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

问题描述

我不是很习惯AS3。我想从txt文件中的一些数据转换为实际的AS3对象。

  1. 如何在加载文本/data/file.txt
  2. 如何查找文本的话?例如:我想找到框架,并检索每个符号意甲(含空格隔开),并将其添加到一个数组/表
解决方案

1

这要看情况。如果该文件是在客户机上,你可以,如果你使用的是AIR仅加载(Flash播放器,它运行了浏览器,不支持这一点)。使用 FileStream.readUTFBytes(),像这样的:

 尝试
{
    var文件:文件=新的文件();
    file.nativePath =该文件的路径,包括文件名;

    如果(file.exists)
    {
        变种FS:的FileStream =新的FileStream();
        尝试
        {
            fs.open(文件,FileMode.READ);
            VAR strFileContents:字符串= fs.readUTFBytes(fs.bytesAvailable);
        }
        赶上(eInner:错误)
        {
            Alert.show(eInner.message);
        }
        最后
        {
            fs.close();
        }
    }
}
赶上(eOuter:错误)
{
    Alert.show(eOuter.message);
}
 

有一个关于这对夫妻的注意事项:如果我没有记错的话,至少除非你找到某种特殊的技巧来做到这一点,该文件的 nativePath的属性需要被设置为绝对路径,或者,如果它是一个相对的路径,它需要是相同的目录中的程序内(它可以是在一个子目录)。这可能是一个安全的事情的Adobe有意实施。同时设置 nativePath的(显式或通过构造函数)可以产生一个错误,所以外try ... catch块也很重要。

如果您通过URL加载文件关闭服务器,您可以使用Flash Player或AIR。只需使用的URLLoader 来代替:

 私人变种m_ldr:的URLLoader =新的URLLoader();

私有函数func()
{
    尝试
    {
        m_ldr.dataFormat = URLLoaderDataFormat.TEXT; // 默认
        m_ldr.addEventListener(引发Event.COMPLETE,ldrDone);
        m_ldr.load(新的URLRequest(文件的URL));
    }
    赶上(五:错误)
    {
    Alert.show(e.message);
    }
}

私有函数ldrDone(pEvent:事件):无效
{
    VAR strContents的:字符串= m_ldr.data;
}
 


2

我不知道我跟你想要做什么用的文字,但你可以拆分字符串到一个数组。如果你想查找的文字框,如果你在这种情况下关心的情况下,你可以做类似如下:

  VAR strString:字符串=框架:在dictionary.com的第一个定义为+
        工字架是\为一个封闭的画面,镜子边框或情况下,+
        等等。\。一个相关的字被成帧和帧也可以被用作动词。
        +...框架;

//注意如何我特别避免缠绕字双引号
// 帧。这使得这更容易,因为现在我们只需要分裂
//空白:

VAR ARRY:阵列= strString.split(/ \ s /); //分割上的所有空白
对于(VAR我:= 0; I< arry.length;我++)
{
    如果(ARRY [我] ==框架)
    {
        跟踪(元素+ I +这个词\框架\ +(I&LT。; arry.length  -  1?
                下一个字是\+ ARRY [I + 1] +。:\));
    }
}

//输出:
//元件10是单词帧。下一个词是是。
//元27是单词帧。下一个词是可以。
//元36是单词帧。
 

I'm not very used to as3. I want to convert some data from txt file to actual as3 object.

  1. How do I load text at "/data/file.txt"
  2. How do I find words in the text? For example: I want to find 'frame' and retrieve every symbol serie (separated with space) and add it into an array/table.

解决方案

1.

It depends. If the file's on the client machine, you can only load it if you're using AIR (Flash Player, which runs out of the browser, does not support this). Use FileStream.readUTFBytes(), like this:

try
{
    var file:File = new File();
    file.nativePath = "the file's path, including the file name";

    if (file.exists)
    {
        var fs:FileStream = new FileStream();
        try
        {
            fs.open(file, FileMode.READ);
            var strFileContents:String = fs.readUTFBytes(fs.bytesAvailable);
        }
        catch (eInner:Error)
        {
            Alert.show(eInner.message);
        }
        finally
        {
            fs.close();
        }
    }
}
catch (eOuter:Error)
{
    Alert.show(eOuter.message);
}

A couple of notes about this: If I'm not mistaken, at least unless you find some sort of special trick to do this, the File's nativePath property needs to be set to either an absolute path or, if it is a relative path, it needs to be within the same directory as the program (it can be in a subdirectory). This is probably a security thing Adobe intentionally implemented. Also setting the nativePath (either explicitly or through the constructor) can raise an error, so the outer try...catch block is important too.

If you're loading the file off a server through a URL, you can use either Flash Player or AIR. Just use a URLLoader instead:

private var m_ldr:URLLoader = new URLLoader();

private function func()
{
    try
    {
        m_ldr.dataFormat = URLLoaderDataFormat.TEXT; // default
        m_ldr.addEventListener(Event.COMPLETE, ldrDone);
        m_ldr.load(new URLRequest("URL of the file"));
    }
    catch (e:Error)
    {
    Alert.show(e.message);
    }
}

private function ldrDone(pEvent:Event):void
{
    var strContents:String = m_ldr.data;
}


2.

I'm not sure I follow what you're wanting to do with the text, but you can split a String into an array. If you want to find the word "frame", and if you care about case in this instance, you can do something like the following:

var strString:String = "Frame: The first definition at dictionary.com for the" +
        "word frame is \"a border or case for enclosing a picture, mirror, " +
        "etc.\".  A related word is framed, and frame can also be used as a verb."
        + " ... frame";

// notice how I specifically avoided wrapping double-quotes around the word
// "frame".  That makes this easier, because now we just have to split on
// whitespace:

var arry:Array = strString.split(/\s/); // splits on all whitespace
for (var i:int = 0; i < arry.length; i++)
{
    if (arry[i] == "frame")
    {
        trace("Element " + i + " is the word \"frame\"." + (i < arry.length - 1 ?
                "  The next word is \"" + arry[i + 1] + "." : "\""));
    }
}

// outputs:
// Element 10 is the word "frame".  The next word is "is".
// Element 27 is the word "frame".  The next word is "can".
// Element 36 is the word "frame".

这篇关于与AS3 txt文件工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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