首页 > 代码库 > AIR中方便的文件操作工具类

AIR中方便的文件操作工具类

最近做项目用到了air 需要随时读写文件,所以封装了一个工具类,在此记录,方便自己和他人日后使用。

FileUtils.as代码:

  1 /////////////////////////////////////////////////////////////////////////////////////////  2 // TXIEJUN Confidential   3 // Copyright 2014. All rights reserved.   4 //   5 // FileUtils.as  6 // Summary // TODO Auto-generated summary stub  7 // Version 1.0  8 // Author txiejun  9 // Created Mar 26, 2014 11:36:27 PM 10 ///////////////////////////////////////////////////////////////////////////////////////// 11 package aurora.utils 12 { 13     import flash.filesystem.File; 14     import flash.filesystem.FileMode; 15     import flash.filesystem.FileStream; 16     import flash.utils.ByteArray; 17     import flash.utils.Endian; 18      19     import mx.controls.Alert; 20  21     public class FileUtils 22     { 23         public function FileUtils() 24         { 25         } 26          27         /** 28          * 保存二进制  29          * @param file 30          * @param byteData 31          * @return  32          *  33          */         34         public static function saveStream(fileOrPath:Object, byteData:ByteArray):Boolean 35         { 36             var result:Boolean = false; 37             if(fileOrPath && byteData) 38             { 39                 var file:File; 40                 if(fileOrPath is String) 41                 { 42                     file = new File(fileOrPath.toString()); 43                 } 44                 else if(fileOrPath is File) 45                 { 46                     file = fileOrPath as File; 47                 } 48                 if(file) 49                 { 50                     var stream:FileStream = new FileStream(); 51                     try 52                     { 53                         trace("SaveStream:", file.nativePath); 54                         stream.open(file, FileMode.WRITE); 55                         stream.writeBytes(byteData); 56                         result = true; 57                     } 58                     catch (e:Error) 59                     { 60                         Alert.show(file.nativePath + "目标文件无法写入,请稍候再试。\n" + e.message); 61                         result = false; 62                     } 63                     finally 64                     { 65                         stream.close(); 66                     } 67                 } 68             } 69              70             return result; 71         } 72  73         /** 74          * 读取二进制  75          * @param file 76          * @return  77          *  78          */         79         public static function readStream(file:File):ByteArray 80         { 81             var result:ByteArray = null; 82             if(file && file.exists) 83             { 84                 var stream:FileStream = new FileStream(); 85                 try 86                 { 87                     stream.open(file, FileMode.READ); 88                     result = new ByteArray(); 89 //                    result.endian = Endian.LITTLE_ENDIAN; 90                     stream.readBytes(result); 91                 } 92                 catch (e:Error) 93                 { 94                     Alert.show(file.nativePath + "读取文件错误。\n" + e.message); 95                 } 96                 finally 97                 { 98                     stream.close(); 99                 }100             }101             102             return result;103         }104         105         /**106          * 保存文本文件107          * @param file108          * @param text109          * @return 110          * 111          */        112         public static function saveText(fileOrPath:Object, text:String):Boolean113         {114             var result:Boolean = false;115             if(fileOrPath)116             {117                 var file:File;118                 if(fileOrPath is String)119                 {120                     file = new File(fileOrPath.toString());121                 }122                 else if(fileOrPath is File)123                 {124                     file = fileOrPath as File;125                 }126                 if(file)127                 {128                     if(text == null)129                     {130                         text = "";131                     }132                     var stream:FileStream = new FileStream();133                     try134                     {135                         trace("saveText:", file.nativePath);136                         stream.open(file, FileMode.WRITE);137                         stream.writeUTFBytes(text);138                         result = true;139                     }140                     catch (e:Error)141                     {142                         Alert.show(file.nativePath + "目标文件无法写入,请稍候再试。\n" + e.message);143                         result = false;144                     }145                     finally146                     {147                         stream.close();148                     }149                 }150             }151             152             return result;153         }154         155         /**156          * 读取文本文件 157          * @param file158          * @return 159          * 160          */        161         public static function readText(file:File):String162         {163             var result:String = "";164             if(file && file.exists)165             {166                 var stream:FileStream = new FileStream();167                 try168                 {169                     stream.open(file, FileMode.READ);170                     result = stream.readUTFBytes(stream.bytesAvailable);171                 }172                 catch (e:Error)173                 {174                     Alert.show(file.nativePath + "读取文件错误。\n" + e.message);175                 }176                 finally177                 {178                     stream.close();179                 }180             }181             182             return result;183         }184 185         /**186          * 保存XML文件187          * @param file188          * @param xml189          * @return 190          * 191          */        192         public static function saveXML(file:File, xml:XML):Boolean193         {194             var result:Boolean = false;195             if(file && xml)196             {197                 var text:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xml.toXMLString();198                 result = saveText(file, text);199             }200             return result;201         }202         203         /**204          * 读取XML文件205          * @param file206          * @return 207          * 208          */        209         public static function readXML(file:File):XML210         {211             var result:XML = null;212             if(file)213             {214                 var text:String = readText(file);215                 result = new XML(text);216             }217             218             return result;219         }220     }221 }