2011-09-26 25 views
1

我嵌入文本文件導入我的Flex項目,並讀取使用代碼的內容是這樣的: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

真的很奇怪!我運行了示例代碼。我的'abc'變量是未定義的。 –

回答

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字符。

+0

我最終使用你的解決方案。我不得不爲文件添加一個換行符。很奇怪:/ –