Question

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.

Was it helpful?

Solution

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();

OTHER TIPS

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");
}

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top