开发者

Flex 3 / Air: Writing blank new lines to files using FileStream

开发者 https://www.devze.com 2022-12-25 06:57 出处:网络
I want to write some text directly to a file using Flex 3 / Air. The text on the file (call it \"Database.txt\") must have the following format:

I want to write some text directly to a file using Flex 3 / Air. The text on the file (call it "Database.txt") must have the following format:

Line1

Line2

Line3

var FS:FileStream = n开发者_Go百科ew FileStream();
var DatabaseFile:File = File.desktopDirectory.resolvePath("Database.txt");
FS.open(DatabaseFile, FileMode.WRITE);
FS.writeUTFBytes("Line1" + "\n" + "Line2" + "\n" + "Line3");
FS.close();

But it writes the following text to the file:

Line1 Line2 Line3.

I'm pretty sure I'm making a very dummy error, but I cannot figure out what it is. Can anyone help me?

Thank you for your time :)


How are you opening Database.txt? If you are using notepad.exe, it will appear all on one line, since notepad.exe is retarded and doesn't support unix line endings (\n). If you absolutely need it to be opened in notepad.exe, what you need to do is use windows line endings instead (\r\n). So your code would look like:

FS.writeUTFBytes("Line1" + "\r\n" + "Line2" + "\r\n" + "Line3");

But now you also have to make sure your code can handle these windows line endings when loading the txt file back into your AIR application (or you might end up with duplicate lines)


FS.writeUTFBytes("Line1" + File.lineEnding + "Line2" + File.lineEnding + "Line3");


byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
FS.Write(newline, 0, newline.Length);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号