我将一个文本文件嵌入到我的Flex项目中,并使用以下代码读取其内容:
[Embed(source = "../../data/abc.txt", mimeType = "application/octet-stream")]
private var r_Abc:Class;
...
var xx:ByteArray = new r_Abc();
var abc:String = xx.toString();
该文件的内容是abc。问题是从文件加载的字符串与其他字符串不可比,即使在调试器中打印或查看时(在FlashDevelop中)它似乎完全正常。
trace(abc); // abc
trace("abc" == abc); // false
如何将其转换为正确的字符串?我尝试使用字符串方法(如substring)来创建副本,但这似乎不是解决方案。
答案 0 :(得分:1)
这是我的样本:
<?xml version="1.0" encoding="utf-8"?>
<s:Application minWidth="955" minHeight="600"
creationComplete="creationCompleteHandler(event)"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.core.ByteArrayAsset;
import mx.events.FlexEvent;
// my file is "ABC "
// strangely enough if I remove the space at the end the string in code is empty
[Embed(source="data/abc.txt", mimeType="application/octet-stream")]
private var abcFile:Class;
protected function creationCompleteHandler(event:FlexEvent):void
{
var abcByteArray:ByteArrayAsset = ByteArrayAsset(new abcFile());
var abc:String = abcByteArray.readUTFBytes(abcByteArray.length);
trace(abc); // ABC (has a space at the end)
trace(abc == "ABC "); // true, but notice the space at the end
}
]]>
</fx:Script>
</s:Application>
我的建议是检查尾随空格,换行。还尝试在文件末尾放置某种EOF字符。