在 AS3 中如何将字符串保存到文件中?

发布于 2024-07-30 18:59:15 字数 57 浏览 8 评论 0 原文

我有一个用户输入的字符串,我想将其保存到用户硬盘上的文件中。 你能做到吗? 如果是这样,怎么办?

I have a string the user has typed and I want to save it into a file on the users harddrive. Can you do that? And if so, how?

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

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

发布评论

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

评论(4

穿越时光隧道 2024-08-06 18:59:15

是的,您可以使用 FileReference。
这基本上就是它的完成方式:

var bytes:ByteArray = new ByteArray();
var fileRef:FileReference=new FileReference();
fileRef.save("fileContent", "fileName");

看起来不太难,不是吗?
这里还有一个视频教程:

http://www.gotoandlearn.com/play?id =76

以及文档:

http://livedocs.adobe.com/flash/ 9.0/ActionScriptLangRefV3/

希望有帮助。

Yes you can, with FileReference.
This is basically how it's done:

var bytes:ByteArray = new ByteArray();
var fileRef:FileReference=new FileReference();
fileRef.save("fileContent", "fileName");

Doesn't look too hard, does it?
And here's a video-tutorial on it too:

http://www.gotoandlearn.com/play?id=76

And the documentation:

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/

Hope that helps.

今天小雨转甜 2024-08-06 18:59:15

由于我有一个将字节输出到文件的函数(因为我正在使用位图做一些事情),所以我也重用它来输出字符串,如下所示:

var filename:String = "/Users/me/path/to/file.txt";
var byteArray:ByteArray = new ByteArray();
byteArray.writeUTFBytes(someString);
outFile(filename, byteArray);

private static function outFile(fileName:String, data:ByteArray):void {
    var outFile:File = File.desktopDirectory; // dest folder is desktop
    outFile = outFile.resolvePath(fileName);  // name of file to write
    var outStream:FileStream = new FileStream();
    // open output file stream in WRITE mode
    outStream.open(outFile, FileMode.WRITE);
    // write out the file
    outStream.writeBytes(data, 0, data.length);
    // close it
    outStream.close();
}

Since I had a function to output bytes to a file (because I was doing something with bitmaps), I reused it to output a string as well, like this:

var filename:String = "/Users/me/path/to/file.txt";
var byteArray:ByteArray = new ByteArray();
byteArray.writeUTFBytes(someString);
outFile(filename, byteArray);

private static function outFile(fileName:String, data:ByteArray):void {
    var outFile:File = File.desktopDirectory; // dest folder is desktop
    outFile = outFile.resolvePath(fileName);  // name of file to write
    var outStream:FileStream = new FileStream();
    // open output file stream in WRITE mode
    outStream.open(outFile, FileMode.WRITE);
    // write out the file
    outStream.writeBytes(data, 0, data.length);
    // close it
    outStream.close();
}
探春 2024-08-06 18:59:15

此外,您的 Flex Builder 3 中还必须安装 Flash Player 10 和 Flex Gumbo SDK。

您还可以查看以下示例:
http://blog.flexexamples.com/2008/08/25/ saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/

In addition, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3.

You can also have a look the following example:
http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/

假装不在乎 2024-08-06 18:59:15

在 Flex 3 中,您无法执行此操作,除非您将文件上传到服务器,然后通过 url 将文件下载到桌面。

在 Air 或 Flex 4 中,您可以将其直接从应用程序保存到桌面,如上所述。

In Flex 3 no you can't do it unless you upload the file to the server and then download the file via a url to the desktop.

In Air or Flex 4 you can save it directly from the application to the desktop as detailed above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文