RSS LinkedIn Twitter

Writing Files with Adobe AIR

March 10th, 2011 Categories: AIR, File I/O, Flex 4, Flex Components

Reading and writing files is something I do in almost every AIR application I write. It has become my language of choice to write batch file processing applications in. Here are a few of the methods I use most commonly when reading and writing files. These are divided into two main groups, binary files and text files. I’ll save reading and writing objects for another post.

When I’m reading a binary file, I usually want it as a ByteArray. You should explore some of the readXXX and writeXXX methods on the FileStream class to see what you can read and write. The functions that process binary files versus the files that process text files are almost identical except for which read/write method is called.

The method readBytes accepts the ByteArray to fill as its parameter.

public static function readBinaryFile(file : File) : ByteArray {
	var stream : FileStream = new FileStream();
	stream.open(file, FileMode.READ);
	var fileBytes : ByteArray = new ByteArray();
	stream.readBytes(fileBytes);
	return fileBytes;
 }

Here is how I write a binary file, whose name is passed in as a parameter. Notice in the previous code snippet I accepted a File reference as a parameter (the processing of the name had been done previously). That process is similar to what you see here: there are a number of objects on the File class that allow you to get a directory. In this case I’m using documentsDirectory, calling resolvePath gets you the pathname you need to create the file reference.

public static function writeBinaryFile(name : String, array : ByteArray) : String {
	try {
		var f : File = File.documentsDirectory.resolvePath(name);
		var fs : FileStream = new FileStream();
		fs.open(f, FileMode.WRITE);
		fs.writeBytes(array);
		fs.close();
		return f.nativePath + " written.";
	}
	catch (err : Error) {
		return err.name;
	}
		return "Error writing file.";
}

Here is the method I use to read text files. Notice that it’s similar to reading a binary file. The readUTFBytes is a handy function if you know you’re dealing with text/strings.

public static function readTextFile(file : File) : String {
	var stream : FileStream = new FileStream();
	stream.open(file, FileMode.READ);
	var s : String = stream.readUTFBytes(stream.bytesAvailable);
	return s;
}

Here is the method I use to create a new text file and write to it.

public static function writeTextFile(name : String, text : String) : String {
	try {
		var f : File = File.documentsDirectory.resolvePath(name);
		var fs : FileStream = new FileStream();
		fs.open(f, FileMode.WRITE);
		fs.writeUTFBytes(text);
		fs.close();
		return f.nativePath + " written.";
	}
	catch (err : Error) {
		return err.name;
	}
	return "Error writing file.";
}

This method is a little different than my other write methods. Normally when I write to a file, I go based off the name. This method is for the use cases where I already have a file reference and need to write to it.

Something else to pay attention to are the FileModes. There are a number of modes to open and/or read a file: APPEND, READ, UPDATE, and WRITE.

public static function writeTextFileFile(file : File, text : String) : String {
	try {
		var fs : FileStream = new FileStream();
		fs.open(file, FileMode.WRITE);
		fs.writeUTFBytes(text);
		fs.close();
		return file.nativePath + " written.";
	}
	catch (err : Error) {
		return err.name;
	}
	return "Error writing file.";
}
Tags:
No comments yet.

Leave a Comment

*