FileIO - Writing binary files in Central 1.5
DIGG IT!
0
Comments
Published
Tuesday, September 21, 2004
at
5:31 AM
.
So Central can read and write files including binary and text files. I am going to explore the methods for working with binary data in Central.&
It is fair to say there are essentially 2 types of files, binary files (PNG, SWF, GIF, EXE, ZIP) and text files( TXT, HTML, XML). Central provides methods in the FileReference Class to work with both types of files.
When you read a binary file in Central, it returns an array filled with byte values of the file in 8-bit blocks. Reading a binary file requires that you start at one point and read to another point in the file. The results contain all the bytes from the start to the end point stored as an array. Here is an example:
//create an instance of the FileReference Class
myBinFile = new FileReference()
//if browse returns a file do this
if (myBinFile.browse(["Flash Movies", "*.swf"])){
swfHeaderArray = myBinFile.readBytes(4)
version = swfHeaderArray[3]
}
In the above example, I used a file reference to prompt the user for a SWF file. If the user selected a SWF file, I read the first 4 bytes from the file and created a variable called verison that denotes the SWF version number from the 4th byte in the file. The first 3 bytes of the SWF format read "FSW" and the version follows as an 8-bit value.
So lets modify this Flash file version value to 7 by using writeBytes on a copy of the file and returning to file to the end user.
//create an instance of the FileReference Class
myBinFile = new FileReference()
//if browse returns a file do this
if (myBinFile.browse(["Flash Movies", "*.swf"])){
//make a copy of the file into the cache
myBinFile.copyIntoCache('workFile.swf')
//close the FileReference
myBinFile.close()
//create a new fileReference
myBinFile2 = new FileReference()
//open the copied swf file
myBinFile2.open('workFile.swf')
//read 4 bytes of the header
swfHeaderArray = myBinFile2.readBytes(4)
// modify the 4th byte to 7
swfHeaderArray[3] = 7
// set the cursor position in the swf file to the start
myBinFile2.setPosition(0)
// overwrite the first 4 bytes of the swf file
myBinFile2.writeBytes(swfHeaderArray)
// propt the user to save the file locally
myBinFile2.saveAs()
// close the file
myBinFile2.close()
}
As you can see you can get fairly advanced with FileReferences and FileIO. Hopefully it wil be useful on your next Central project.
Cheers,
Ted ;)

0 Responses to “ FileIO - Writing binary files in Central 1.5 ”
Post a Comment