我正在使用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文件的编码有关。有没有人有办法解决这个问题?提前感谢!
发布于 2011-10-30 16:39:27
从文件读取文本信息时,应始终使用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();https://stackoverflow.com/questions/7911070
复制相似问题