在前一篇文章里,我们讨论了如何利用FileIO从非文本文件中读取数据的问题,本次我们讨论用FileIO编辑现有文件,例如MP3文件上,或是创建新文件,例如从Director 的cast member中输出位图文件。
编辑MP3文件
写二进制文件最简单的例子莫过于编辑MP3文件的标签。这总比新建文件要来得简单,你不必去弄清整个文件结构,只需明白歌曲信息是储存在最后128个字节里。
on writeBinaryFile filePath, byteList, fileStartByte, listStartByte, deleteOriginalFile
result = 0 fileObj = new (xtra "fileIO") if deleteOriginalFile then -- It's often "cleaner" to remove the existing file -- though you obviously wouldn't do this when editing -- an MP3 file fileObj.openFile (filePath, 0) fileObj.delete () fileObj.createFile (filePath) end if fileObj.openFile (filePath, 2) -- Attempt to open the file with write-only access
if fileObj.status () = 0 then listLength = byteList.count if fileStartByte.voidP then fileStartByte = 1 -- Set default positions if none were supplied via -- the fileStartByte & listStartByte parameters if listStartByte.voidP then listStartByte = 1
fileObj.setPosition (fileStartByte-1) -- Set the write position in the file & begin dumping -- the list contents into the file
repeat with index = listStartByte to listLength fileObj.writeChar (numToChar (byteList[index])) end repeat result = 1 end if
fileObj.closeFile () fileObj = 0 return result end
[1] [2] 下一页 |