0

我正在使用 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 文件的编码有关。有没有人对如何解决这个问题有任何想法?提前致谢!

4

1 回答 1

0

You should always use stream.readUTFBytes() or stream.readUTF() when reading text information from files. I'm guessing some actual encoding issues arise when you convert bytes to string in your code. The correct code would be:

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();
于 2011-10-30T08:39:27.413 回答