AS3 / AIR - Creating a plain text file?
Asked Answered
W

3

13

is it possible to create a plain text file with AS3 or AIR?

example: i would like to create a plain text file named "MyTextFile.txt", have it contain text that reads "This is my text file." and save it to my desktop.

another option would be to have the file already exist in a directory, so i would only have to rewrite its contents - assuming that would be easier.

all of which should happen as a background process, without any save dialoge panel appearing.

Wescott answered 29/9, 2010 at 15:37 Comment(0)
P
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();
Perplexed answered 29/9, 2010 at 15:55 Comment(0)
S
3

I know this is an old post, but consider the following to create a new .txt file from an input text field's text.

var tf:TextField;
var fileRef:FileReference;

function saveFile(evt):void
{
fileRef = new FileReference();
fileRef.save(tf.text, "saveFile.txt");
}
Slog answered 6/9, 2011 at 15:20 Comment(0)
N
0

Also consider this text:

Text fields instead of trace statements

When running on a mobile device, you cannot see the output from the trace statements.

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; 

}

And so forth...

Nullity answered 12/2, 2014 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.