实现将windows系统中创建的txt文件批量转换为utf-8编码。
使用flash air技术实现。没有对编码做深入研究,不敢保证转码一定正确,所以一定不要覆盖掉源文件。
工具界面如下:
主要代码如下。
Ansi2Utf8.as
package { import com.adobe.images.PNGEncoder; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.display.MovieClip; import flash.display.SimpleButton; import flash.events.Event; import flash.events.MouseEvent; import flash.filesystem.File; import flash.geom.Matrix; import flash.geom.Rectangle; import flash.net.FileFilter; import flash.net.URLLoader; import flash.text.TextField; import flash.utils.ByteArray; import flash.utils.setTimeout; /** * ... */ public class Ansi2Utf8 extends MovieClip { public var __browse:SimpleButton; public var __outPut:SimpleButton; public var __filesTf:TextField; public var __outTf:TextField; private var _files:Array; private var _customFile:CustomFile ; public function Ansi2Utf8() { stage?initStage():addEventListener(Event.ADDED_TO_STAGE, initStage); } public function hide():void { visible = false; } public function show():void { visible = true; } private function initStage(e:Event=null):void { removeEventListener(Event.ADDED_TO_STAGE, initStage); _customFile = new CustomFile(); __browse.addEventListener(MouseEvent.CLICK, onBrowse); __outPut.addEventListener(MouseEvent.CLICK, onOutPut); } /** * 输出点击 * @param e */ private function onOutPut(e:MouseEvent):void { if (!_files) { return; } _customFile.browseDirector("保存路径", saveDirectorSelected); } /** * 输出目录选择完毕 * @param file * @param files */ private function saveDirectorSelected(file:File,files:Array):void { __outTf.text = ""; outPut("选择输出目录:" + file.nativePath ); var f:File; var bytes:ByteArray; var ld:Loader; var bmd:BitmapData; var rect:Rectangle; var bmp:Bitmap; var imgName:String; var obj:Array = []; var curId:int = 0; loadAndDo(); function loadAndDo():void { f = _files[curId] as File; imgName = getFileName(f.nativePath); outPut("==="+imgName+"==="); outPut("加载文件;"); bytes = _customFile.getBytesFromFile(f); outPut("处理文件;"); var txt:String = bytes2string(bytes); var b:ByteArray = new ByteArray(); b.writeMultiByte(txt, "utf-8"); obj.push("======================================"); obj.push(imgName); obj.push(txt); outPut("保存文件;" ); _customFile.saveBytes(b, new File(file.nativePath + "\\" + imgName)); outPut("文件保存完成。"); curId++; if (curId < _files.length) { //loadAndDo(); setTimeout(loadAndDo, 1); } else { outPut("保存log,可以从里边查看是否有乱码"); _customFile.saveText(obj.join("\r\n"), new File(file.nativePath + "\\log.txt")); outPut("保存完成"); } } } /** * * @param bytes * @return */ private function bytes2string(bytes:ByteArray):String { bytes.position = 0; var txt:String; switch(int(bytes[0]).toString(16) + int(bytes[1]).toString(16)) { case "fffe"://Unicode //trace("Unicode"); txt = bytes.readMultiByte(bytes.bytesAvailable, "unicode"); break; case "feff"://Unicode big endian //trace("Unicode big endian"); txt = bytes.readMultiByte(bytes.bytesAvailable, "unicodeFFFE"); break; case "efbb"://UTF-8 //trace("UTF-8"); txt = bytes.readMultiByte(bytes.bytesAvailable, "utf-8"); break; default://ANSI //trace("ANSI"); txt = bytes.readMultiByte(bytes.bytesAvailable, "gb2312"); var b:ByteArray = new ByteArray(); b.writeByte(0xef); b.writeByte(0xbb); b.writeByte(0xbf); b.writeMultiByte(txt, "utf-8"); b.position = 0; txt = b.readMultiByte(b.bytesAvailable, "utf-8"); break; } return txt; } private function outPut(msg:String):void { __outTf.appendText(msg + "\n"); __outTf.scrollV = __outTf.maxScrollV; } /** * 浏览文件 * @param e */ private function onBrowse(e:MouseEvent):void { _customFile.browseMultiple("浏览目录", [new FileFilter("所有文件(*)", "*") ,new FileFilter("txt文件(*.txt)", "*.txt") ,new FileFilter("xml文件(*.xml)", "*.xml") ,new FileFilter("网页文件(*.htm;*.html;*.php;*.asp;*.jsp)", "*.htm;*.html;*.php;*.asp;*.jsp")] ,browseSelected); } /** * 浏览文件选择完成 * @param files */ private function browseSelected(files:Array):void { var s:String = ""; var fileName:String; for each(var f:File in files) { fileName = getFileName(f.nativePath); s += fileName + "\n"; } __filesTf.text = s; _files = files; } /** * 文件路径中提取文件名 * @param path * @return */ private function getFileName(path:String):String { var fileName:String=path; if (fileName.indexOf("\\") >= 0) { fileName = fileName.substring(fileName.lastIndexOf("\\")+1, fileName.length); } return fileName; } } }
CustomFile.as
package { import flash.events.Event; import flash.events.FileListEvent; import flash.events.IOErrorEvent; import flash.filesystem.File; import flash.filesystem.FileMode; import flash.filesystem.FileStream; import flash.utils.ByteArray; /** * ... */ public class CustomFile extends Object { public var file:File = new File(); public var fileStream:FileStream = new FileStream(); private var _callBack:Function; private var browseTyle:String; public function CustomFile() { file.addEventListener(Event.CANCEL, onCancel); file.addEventListener(IOErrorEvent.IO_ERROR, onError); file.addEventListener(Event.SELECT, onSelect); file.addEventListener(FileListEvent.SELECT_MULTIPLE, onSelectMultiple); } /** * 浏览并选择目录 * @param title * @param callBack */ public function browseDirector(title:String, callBack:Function):void { file.browseForDirectory(title); _callBack = callBack; browseTyle = "director"; } /** * 浏览并选择单个文件 * @param title * @param typeFilter * @param callBack */ public function browseFile(title:String, typeFilter:Array, callBack:Function):void { file.browseForOpen(title, typeFilter); _callBack = callBack; browseTyle = "file"; } /** * 浏览并选择多个文件 * @param title * @param typeFilter * @param callBack */ public function browseMultiple(title:String, typeFilter:Array, callBack:Function):void { file.browseForOpenMultiple(title, typeFilter); _callBack = callBack; browseTyle = "multiple"; } /** * 浏览保存位置 * @param title * @param callBack */ public function browseSave(title:String, callBack:Function):void { file.browseForSave(title); _callBack = callBack; browseTyle = "save"; } /** * 保存字符串 * @param s * @param file */ public function saveText(s:String, file:File):void { fileStream.open(file, FileMode.WRITE); fileStream.writeUTFBytes(s); fileStream.close(); } /** * 保存二进制 * @param b * @param file */ public function saveBytes(b:ByteArray, file:File):void { fileStream.open(file, FileMode.WRITE); fileStream.writeBytes(b); fileStream.close(); } /** * 以二进制形式读取文件 * @param file * @return */ public function getBytesFromFile(file:File):ByteArray { var bytes:ByteArray = new ByteArray(); fileStream.open(file, FileMode.READ); fileStream.readBytes(bytes, 0, fileStream.bytesAvailable); fileStream.close(); return bytes; } /** * 选择了单个文件或目录 * @param e */ private function onSelect(e:Event):void { switch(browseTyle) { case "file": _callBack(file); break; case "director": _callBack(file,file.getDirectoryListing()); break; case "save": _callBack(file); break; default: trace("select error"); break; } } /** * 选择了多个文件 * @param e */ private function onSelectMultiple(e:FileListEvent):void { _callBack(e.files); } /** * 取消 * @param e */ private function onCancel(e:Event):void { } /** * 错误 * @param e */ private function onError(e:IOErrorEvent):void { trace("在此平台上不支持浏览操作"); } } }
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。