Flex 3 / Air:使用 FileStream 将空白新行写入文件

发布于 2024-08-27 18:06:41 字数 479 浏览 6 评论 0原文

我想使用 Flex 3 / Air 将一些文本直接写入文件。文件上的文本(称为“Database.txt”)必须具有以下格式:

Line1

Line2

Line3

var FS:FileStream = new FileStream();
var DatabaseFile:File = File.desktopDirectory.resolvePath("Database.txt");
FS.open(DatabaseFile, FileMode.WRITE);
FS.writeUTFBytes("Line1" + "\n" + "Line2" + "\n" + "Line3");
FS.close();

但它将以下文本写入文件:

Line1 Line2 Line3。

我很确定我犯了一个非常愚蠢的错误,但我无法弄清楚它是什么。谁能帮助我吗?

谢谢您的宝贵时间:)

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 = new 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筱武穆 2024-09-03 18:06:41

你如何打开Database.txt?如果您使用的是 notepad.exe,它将全部显示在一行上,因为 notepad.exe 速度较慢且不支持 UNIX 行结尾 (\n)。如果您确实需要在 notepad.exe 中打开它,则需要使用 Windows 行结尾 (\r\n)。因此,您的代码将如下所示:

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

但现在您还必须确保您的代码在将 txt 文件加载回 AIR 应用程序时可以处理这些 Windows 行结尾(否则您可能会得到重复的行)

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)

宫墨修音 2024-09-03 18:06:41

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

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

近箐 2024-09-03 18:06:41
byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
FS.Write(newline, 0, newline.Length);
byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
FS.Write(newline, 0, newline.Length);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文