AS3 / AIR - 如何创建纯文本文件?

13

是否可以使用AS3或AIR创建纯文本文件?

例如:我想要创建一个名为"MyTextFile.txt"的纯文本文件,其中包含文本"This is my text file."并将其保存到我的桌面。

另一种选择是让文件已经存在于目录中,因此我只需重写其内容 - 假设这样做会更容易。

所有这些都应该作为后台进程发生,而不会出现任何保存对话框面板。

3个回答

28
var file:File = File.desktopDirectory.resolvePath("MyTextFile.txt");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes("This is my text file.");
stream.close();

3

我知道这是一个老帖子,但考虑以下方法从输入文本字段的文本创建一个新的 .txt 文件。

var tf:TextField;
var fileRef:FileReference;

function saveFile(evt):void
{
fileRef = new FileReference();
fileRef.save(tf.text, "saveFile.txt");
}

0

还要考虑这段文字:

使用文本字段代替跟踪语句

在移动设备上运行时,您无法看到跟踪语句的输出。

function createTracingTextField(x:Number, y:Number, width:Number, height:Number):TextField {

var tracingTF:TextField = new TextField(); 
tracingTF.x = x; 
tracingTF.y = y; 
tracingTF.width = width; 
tracingTF.height = height; 

// A border lets you more easily see the area the text field covers. 
tracingTF.border = true; 
// Left justifying means that the right side of the text field is automatically 
// resized if a line of text is wider than the width of the text field. 
// The bottom is also automatically resized if the number of lines of text 
// exceed the length of the text field. 
tracingTF.autoSize = TextFieldAutoSize.LEFT; 

// Use a text size that works well on the device. 
var myFormat:TextFormat = new TextFormat(); 
myFormat.size = 18; 
tracingTF.defaultTextFormat = myFormat; 

addChild(tracingTF); 
return tracingTF; 

}

等等,诸如此类...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接