以编程方式编辑.webarchive文件

时间:2011-10-27 02:21:34

标签: actionscript-3 flex safari flash-builder webarchive

我正在使用Actionscript构建AIR应用程序,我想以编程方式将一段文本插入到.webarchive文件中。问题是每次插入文本时,文件都会以某种方式被破坏。我正在使用的代码如下所示:

var stream:FileStream = new FileStream();                       
stream.open(file, FileMode.READ);   
var body:ByteArray = new ByteArray();                       
stream.readBytes(body, file.size);                      
var result:Array = pattern.exec(body.toString());                   
var new_body:String;                        
new_body = body.toString().replace(pattern, "replacing text here!</body>"); 
stream.close();                     
stream.open(file, FileMode.WRITE);                      
stream.writeUTFBytes(new_body);                     
stream.close();

我猜这个问题与.webarchive文件的编码有关。有没有人有任何想法如何解决这个问题?提前谢谢!

1 个答案:

答案 0 :(得分:0)

从文件中读取文本信息时,应始终使用stream.readUTFBytes()stream.readUTF()。我猜你在代码中将字节转换为字符串时会出现一些实际的编码问题。正确的代码是:

var stream:FileStream = new FileStream();                       
stream.open(file, FileMode.READ);   
var body:String = stream.readUTFBytes(stream.bytesAvailable);   
stream.close();               
var new_body:String = body.replace(pattern, "replacing text here!</body>");  
stream.open(file, FileMode.WRITE);                      
stream.writeUTFBytes(new_body);                     
stream.close();
相关问题