简要:主要是对File类与FileStream类的使用
创建的是桌面程序。
代码如下:
1 <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
2 xmlns:s="library://ns.adobe.com/flex/spark"
3 xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="windowedapplication1_creationCompleteHandler(event)">
4
5 <fx:Script>
6 <![CDATA[
7
8 import mx.core.UIComponent;
9 import mx.events.FlexEvent;
10
11 public var file2:File;
12 public var isOpen:Boolean=false;
13 public var fs:FileStream;
14
15 protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
16 {
17 //应用程序的相关信息
18 out("应用程序目录:"+File.applicationDirectory.nativePath);
19 out("应用程序存储目录:"+File.applicationStorageDirectory.nativePath);
20 out("桌面目录:"+File.desktopDirectory.nativePath);
21 out("文档目录:"+File.documentsDirectory.nativePath);
22 out("用户目录:"+File.userDirectory.nativePath);
23 }
24
25 public function out(txt:Object):void
26 {
27 info.appendText(txt.toString()+"\n");
28 }
29 //文件浏览
30 protected function Bws_clickHandler(event:MouseEvent):void
31 {
32 var file:File=new File();
33 var txt:FileFilter=new FileFilter("文档文件","*.txt;*.docx;*.zip");
34 var jpg:FileFilter=new FileFilter("图片文件","*.swf; *.gif");
35 file.browse([txt,jpg]);
36 file.addEventListener(Event.SELECT,onSelect);
37 file.addEventListener(Event.CANCEL,onCancel);
38 }
39 //文件浏览选中事件
40 public function onSelect(evt:Event):void
41 {
42 var file:File=evt.target as File;
43 out("选择:"+file.nativePath);
44 }
45 //文件浏览取消事件
46 public function onCancel(evt:Event):void
47 {
48 out("文件选择被取消");
49 }
50 //打开浏览
51 protected function Open_clickHandler(event:MouseEvent):void
52 {
53 var file:File=new File();
54 var txt:FileFilter=new FileFilter("文档文件","*.txt;*.doc");
55 var jpg:FileFilter=new FileFilter("图片文件","*.png;*.gif");
56 file.browseForOpen("打开文件名",[txt,jpg]);
57 file.addEventListener(Event.SELECT,onSelect);
58 file.addEventListener(Event.CANCEL,onCancel);
59 }
60 //保存浏览
61 protected function Save_clickHandler(event:MouseEvent):void
62 {
63 var file:File=File.desktopDirectory;
64 file.browseForSave("保存测试文件");
65 file.addEventListener(Event.SELECT,onSelect);
66 file.addEventListener(Event.CANCEL,onCancel);
67 }
68 //多文档浏览。一次可以打开多个文件
69 protected function MulFiles_clickHandler(event:MouseEvent):void
70 {
71 var file:File=new File();
72 var txt:FileFilter=new FileFilter("文档文件","*.txt;*.doc");
73 var jpg:FileFilter=new FileFilter("图片文件","*.png;*.gif");
74 file.browseForOpenMultiple("寻找目标",[txt,jpg]);
75 file.addEventListener(FileListEvent.SELECT_MULTIPLE,onMulSelect);
76 file.addEventListener(Event.CANCEL,onCancel);
77 }
78
79 public function onMulSelect(evt:FileListEvent):void
80 {
81 for each (var file:File in evt.files)
82 {
83 out("选择"+file.nativePath);
84 }
85 }
86
87 //目录浏览
88 protected function MulCal_clickHandler(event:MouseEvent):void
89 {
90 var file:File=File.desktopDirectory;
91 file.browseForDirectory("寻找目标");
92 file.addEventListener(Event.SELECT,onSelect);
93 file.addEventListener(Event.CANCEL,onCancel);
94 }
95 //快速保存文件 , 以下代码 新建了一个setup.txt的文件。然后写入了一段字符串后,保存整个文件。
96 protected function FastSave_clickHandler(event:MouseEvent):void
97 {
98 var file:File=File.applicationStorageDirectory.resolvePath("setup.txt");
99 var str:String="this is save test"+(new Date());
100 file.addEventListener(Event.COMPLETE,onComplete);
101 file.save(str);
102 }
103
104 public function onComplete(evt:Event):void
105 {
106 out("保存完成");
107 }
108 //快速载入,指定文件所在位置,直接打开,并把信息显示出来
109 protected function FastLoad_clickHandler(event:MouseEvent):void
110 {
111 var file:File=File.applicationStorageDirectory.resolvePath("C:\\Users\\Administrator\\Desktop\\setup.txt"); //文件所在路径,需转义
112 file.addEventListener(Event.COMPLETE,dataLoaded);
113 out(file.nativePath);
114 file.load();
115 function dataLoaded(evt:Event):void
116 {
117 var str:String;
118 var bytes:ByteArray=file.data;
119 // str=bytes.readUTFBytes(bytes.length); // 出现中文乱码错误
120 str=bytes.readMultiByte(bytes.length,"cngb");
121 out(str);
122 }
123 }
124 //浏览文件并查看文件属性
125 protected function fileProp_clickHandler(event:MouseEvent):void
126 {
127 var file:File=new File();
128 file.browse();
129 file.addEventListener(Event.SELECT,onFileProSelect);
130 }
131
132 public function onFileProSelect(evt:Event):void
133 {
134
135 var uic:UIComponent=new UIComponent(); //用来显示文件的图标
136 var file:File=evt.target as File;
137 out("存在:"+file.exists);
138 //显示被选择文件图标
139 var iconData:Array=file.icon.bitmaps;
140 var bmpData:BitmapData=new BitmapData(1,1);
141 //复制位图数据
142 for(var i:uint=0;i<iconData.length;i++)
143 {
144 if(iconData[i].height > bmpData.height)
145 {
146 bmpData=file.icon.bitmaps[i];
147 }
148 }
149 var iconImg:Bitmap=new Bitmap(bmpData);
150 uic.x=800;
151 uic.y=100;
152 uic.addChild(iconImg);
153 addElement(uic);
154
155 out("隐藏:"+file.isHidden);
156 out("目录:"+file.isDirectory);
157 out("包:"+file.isPackage);
158 out("快捷方式:"+file.isSymbolicLink);
159 out("可用空间:"+(file.spaceAvailable/1024/1024)+"MB");
160 out("上级目录:"+file.parent.nativePath);
161 }
162 //创建文件与目录
163 protected function CreateFiles_clickHandler(event:MouseEvent):void
164 {
165 var newDir:File=File.desktopDirectory.resolvePath("ZZZ");
166 try
167 {
168 newDir.createDirectory(); //创建目录, 在桌面上创建了一个ZZZ的文件夹
169 out(newDir.nativePath);
170 }
171 catch(event:IOError)
172 {
173 out("创建目录方式错误");
174 }
175
176 var newTempDir:File=File.createTempDirectory(); //创建临时目录,在C:\Users\Administrator\AppData\Local\Temp 下
177 out(newTempDir.nativePath);
178
179 var newTempFile:File=File.createTempFile(); //创建临时文件 在C:\Users\Administrator\AppData\Local\Temp 下
180 out(newTempFile.nativePath);
181 }
182 //删除目录 (同步方式)
183 protected function delete_clickHandler(event:MouseEvent):void
184 {
185 try
186 {
187 var newDir:File=File.desktopDirectory.resolvePath("E:\\ZZZ"); //删除 E:\ZZZ的文件夹
188 newDir.deleteDirectory(true);
189 out("目录已经被删除");
190 }
191 catch(event:IOError)
192 {
193 out("目录删除方式错误.\n"+event); //如果文件不存在,则报错。 Error: Error #3003: File or directory does not exist.
194 }
195 }
196 //删除目录(异步方式)
197 protected function delete2_clickHandler(event:MouseEvent):void
198 {
199 var newDir:File=File.desktopDirectory.resolvePath("ZZZ");
200 newDir.addEventListener(Event.COMPLETE,deleComp);
201 newDir.addEventListener(IOErrorEvent.IO_ERROR,onIOERR);
202 function deleComp(event:Event):void
203 {
204 out("目录已被删除!");
205 }
206 function onIOERR(event:IOErrorEvent):void
207 {
208 out("目录删除失败!"+event);
209 }
210 newDir.moveToTrashAsync(); //删除到回收站
211 }
212
213 protected function deleteAll_clickHandler(event:MouseEvent):void
214 {
215 info.text="";
216 }
217 //异步模式打开文件,并显示读取到的文件
218 protected function openAsync_clickHandler(event:MouseEvent):void
219 {
220 var txt:FileFilter=new FileFilter("文本文件","*.txt;");
221 file2=new File();
222 file2.addEventListener(Event.SELECT,onFile2Select);
223 file2.addEventListener(Event.CANCEL,onCancel);
224 file2.browseForOpen("异步模式打开文本文件",[txt]);
225 }
226 //选中文件并打开
227 public function onFile2Select(evt:Event):void
228 {
229 if(isOpen)
230 {
231 fs.close();
232 }
233 fs=new FileStream();
234 fs.addEventListener(Event.COMPLETE,onReadComp);
235 fs.addEventListener(IOErrorEvent.IO_ERROR,onError);
236 fs.addEventListener(ProgressEvent.PROGRESS,onPrg);
237 fs.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS,onOutprg);
238 fs.addEventListener(Event.CLOSE,onClose);
239 fs.openAsync(file2,FileMode.UPDATE); //异步更新方式打开文件
240 isOpen=true;
241 this.title=file2.name;
242 }
243 //文件打开完成事件
244 public function onReadComp(evt:Event):void
245 {
246 out("文件已经打开并开始读取");
247 var str:String=fs.readMultiByte(fs.bytesAvailable,"GBK");
248 isOpen=true;
249 out(str);
250 fs.removeEventListener(Event.COMPLETE,onReadComp);
251 }
252
253 public function onError(evt:IOErrorEvent):void
254 {
255 out("文件读写异常");
256 }
257 //写入进度处理
258 public function onOutprg(evt:OutputProgressEvent):void
259 {
260 out("输出进度:"+evt.bytesPending+"/"+evt.bytesTotal);
261 }
262 //打开进度处理
263 public function onPrg(evt:ProgressEvent):void
264 {
265 out("打开进度:"+evt.bytesTotal+"/"+evt.bytesLoaded);
266 }
267
268 public function onClose(evt:Event):void
269 {
270 out("关闭");
271 }
272
273 ]]>
274 </fx:Script>
275
276 <fx:Declarations>
277 <!-- 将非可视元素(例如服务、值对象)放在此处 -->
278 </fx:Declarations>
279 <s:VGroup gap="20">
280
281 <s:Button label="清除" click="deleteAll_clickHandler(event)"/>
282
283 <s:TextArea id="info" width="100%" height="100"/>
284 <!--多种方式浏览本地文件夹-->
285 <s:HGroup gap="20">
286 <s:Button label="浏览" click="Bws_clickHandler(event)"/>
287 <s:Button label="浏览(打开)" click="Open_clickHandler(event)"/>
288 <s:Button label="浏览(保存)" click="Save_clickHandler(event)"/>
289 <s:Button label="浏览(目录)" click="MulCal_clickHandler(event)"/>
290 <s:Button label="浏览(多文档)" click="MulFiles_clickHandler(event)"/>
291 <s:Button label="异步模式打开文件" click="openAsync_clickHandler(event)"/>
292 </s:HGroup>
293
294 <s:HGroup gap="20">
295 <s:Button label="快速保存" click="FastSave_clickHandler(event)"/>
296 <s:Button label="快速载入" click="FastLoad_clickHandler(event)"/>
297 <s:Button label="查看文件属性" click="fileProp_clickHandler(event)"/>
298 </s:HGroup>
299
300 <s:HGroup gap="20">
301 <s:Button label="创建文件与目录" click="CreateFiles_clickHandler(event)"/>
302 <s:Button label="删除(同步方式)" click="delete_clickHandler(event)"/>
303 <s:Button label="删除(异步方式)" click="delete2_clickHandler(event)"/>
304 </s:HGroup>
305 </s:VGroup>
306 </s:WindowedApplication>
@简道云 原创内容,如需转载,请一定署名!否则视为侵权!特此声明!