Saving string to text file in AS3
Asked Answered
R

2

2

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:enter image description here

What am I doing wrong?

Radiography answered 7/3, 2014 at 4:28 Comment(0)
R
7

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.

Rawley answered 7/3, 2014 at 4:44 Comment(0)
E
3

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

Encrata answered 7/3, 2014 at 4:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.