2010-09-29 8 views
11

est-il possible de créer un fichier texte brut avec AS3 ou AIR? Exemple: Je voudrais créer un fichier texte brut nommé "MyTextFile.txt", il doit contenir du texte qui lit "Ceci est mon fichier texte." et enregistrez-le sur mon bureau.AS3/AIR - Création d'un fichier texte brut?

une autre option serait d'avoir le fichier déjà existant dans un répertoire, donc je n'aurais qu'à réécrire son contenu - en supposant que ce serait plus facile.

ce qui devrait se faire en arrière-plan, sans qu'aucun panneau de dialogue de sauvegarde apparaisse.

Répondre

22
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

Je sais qu'il s'agit d'un ancien article, mais considérez ce qui suit pour créer un nouveau fichier .txt à partir du texte d'un champ de saisie.

var tf:TextField; 
var fileRef:FileReference; 

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

Voir également ce texte:

Text fields instead of trace statements

Lors de l'exécution sur un appareil mobile, vous ne pouvez pas voir la sortie des relevés de traces.

createTracingTextField fonction

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

}

Et ainsi de suite ...