标签:style blog color 使用 os io 文件 ar
最近做项目用到了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 file 108 * @param text 109 * @return 110 * 111 */ 112 public static function saveText(fileOrPath:Object, text:String):Boolean 113 { 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 try 134 { 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 finally 146 { 147 stream.close(); 148 } 149 } 150 } 151 152 return result; 153 } 154 155 /** 156 * 读取文本文件 157 * @param file 158 * @return 159 * 160 */ 161 public static function readText(file:File):String 162 { 163 var result:String = ""; 164 if(file && file.exists) 165 { 166 var stream:FileStream = new FileStream(); 167 try 168 { 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 finally 177 { 178 stream.close(); 179 } 180 } 181 182 return result; 183 } 184 185 /** 186 * 保存XML文件 187 * @param file 188 * @param xml 189 * @return 190 * 191 */ 192 public static function saveXML(file:File, xml:XML):Boolean 193 { 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 file 206 * @return 207 * 208 */ 209 public static function readXML(file:File):XML 210 { 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 }
标签:style blog color 使用 os io 文件 ar
原文地址:http://www.cnblogs.com/txiejun/p/3916272.html