Saving string to text file in AS3

让人想犯罪 __ 提交于 2020-01-15 05:46:39

问题


I have a function for writing String to a text file. It works, but for some reason (encoding?) it adds some weird data to the beginning of the file.

Here is the relevant code:

    var file:File =  new File(OUTPUT_FILE_NAME);
    var stream:FileStream = new FileStream();
    stream.open(file, FileMode.WRITE);
    stream.writeUTF("Hello World!");
    stream.close();

When I open the file in Notepad++, it shows this:

What am I doing wrong?


回答1:


use this stream.writeUTFBytes("Hello World"); instead of stream.writeUTF("Hello World!");

if we use writeUTF then it write prefix the string with a 16-bit length word. if you use writeUTFBytes then it omit this prefix Length String.




回答2:


writeUTF prepends the length of the string before writing it to file. Use writeUTFBytes if you want to write only the string.



来源:https://stackoverflow.com/questions/22241454/saving-string-to-text-file-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!