1. 程式人生 > >AS3 - 對文件和目錄的操作

AS3 - 對文件和目錄的操作

direct 目錄 文件夾 var pad append string gin number

1,寫入到文件
1 2 3 4 5 var fileObj:File = File.documentsDirectory.resolvePath("hangge.txt"); var fs:FileStream = new FileStream(); fs.open(fileObj, FileMode.WRITE); fs.writeUTFBytes("hangge.com"); fs.close();

2,更新文件內容
(1)下面例子在第5個字節開始覆寫內容

1 2 3 4 5 6 var fileObj:File = File.documentsDirectory.resolvePath(
"hangge.txt"); var fs:FileStream = new FileStream(); fs.open(fileObj, FileMode.UPDATE); fs.position = 5; fs.writeUTFBytes(".yuhang"); fs.close();

(2)下面例子將內容添加到文件末尾

1 2 3 4 5 var fileObj:File = File.documentsDirectory.resolvePath("hangge.txt"); var fs:FileStream = new FileStream();
fs.open(fileObj, FileMode.APPEND); fs.writeUTFBytes("www.hangge.com"); fs.close();

3,讀取文件內容

1 2 3 4 5 var fileObj:File = File.documentsDirectory.resolvePath("hangge.txt"); var fs:FileStream = new FileStream(); fs.open(fileObj, FileMode.READ); var str:String = fs.readUTFBytes(fs.bytesAvailable);
fs.close();

4,移動文件

1 2 3 var originalFile:File = File.documentsDirectory.resolvePath("hangge.txt"); var newDir:File = File.applicationStorageDirectory.resolvePath("hangge.txt"); originalFile.moveTo(newDir);

5,復制文件

1 2 3 var originalFile:File = File.documentsDirectory.resolvePath("hangge.txt"); var newDir:File = File.applicationStorageDirectory.resolvePath("hangge.txt"); originalFile.copyTo(newDir);

6,刪除文件

1 2 3 4 var fileObj:File = File.documentsDirectory.resolvePath("hangge.txt"); if(fileObj.exists){ fileObj.deleteFile(); }

7,創建文件夾

1 2 var fileDir:File = File.documentsDirectory.resolvePath("hangge"); fileDir.createDirectory();

8,移動文件夾

1 2 3 var originalDir:File = File.documentsDirectory.resolvePath("hangge"); var newDir:File = File.applicationStorageDirectory.resolvePath("hangge"); originalDir.moveTo(newDir);

9,復制文件夾

1 2 3 var originalDir:File = File.documentsDirectory.resolvePath("hangge"); var newDir:File = File.applicationStorageDirectory.resolvePath("hangge"); originalDir.copyTo(newDir);

10,刪除文件夾

1 2 3 4 var fileDir:File = File.documentsDirectory.resolvePath("hangge"); if(fileDir.exists){ fileDir.deleteDirectory(); }

AS3 - 對文件和目錄的操作