AIRでのファイル、ディレクトリ操作
久しぶりにAIRがやりたくなったので、まだ自分が試したことがないファイルとディレクトリ操作に関して勉強しみました
勉強した部分のまとめ
よく使いそうなFileクラスのプロパティ
exists: 指定したファイル、フォルダが存在するかどうかを調べる
isDirectory: フォルダかどうかを調べる
parent: 親フォルダを調べる
size: ファイルサイズを調べる
desktopDirectory: デスクトップへアクセス
documentDirectory: ドキュメントディレクトリへアクセス
userDirectory: ユーザーフォルダにアクセス
メソッド
copyTo: ファイル、フォルダをコピーする
createDirectory: フォルダの生成
createTempDirectory, createTempFile: 一時フォルダ、ファイルの生成
deleteDirectory, deleteFile: フォルダ、ファイルの削除
moveTo: ファイル、フォルダの移動
moveToTrash: ファイル、フォルダをゴミ箱へ移動
resolvePath: desktopDirectoryなどを使用してファイル名を使用したとき、そのファイルの実際のパスに基ずくファイルオブジェクトを生成する。
実際に使ってみた↓
一時ファイルの作成、ファイルの親フォルダを調べる
var tempfile:File = File.createTempFile(); trace(tempfile.exists); //作成した一時ファイルが存在するかどうか。 trueを出力 trace(tempfile.parent.nativePath); //親フォルダのPathを出力 tempfile.deleteFile(); //ファイルの削除 trace(tempfile.exists); //falseを出力 /* 出力 true C:\Documents and Settings\User\Local Settings\Temp false */
ユーザーフォルダにあるファイルとフォルダの一覧を配列に格納
var userDirAndFiles:Array = File.userDirectory.getDirectoryListing(); //配列の内容を一つずつフォルダかどうか調べる for(var i:uint = 0; i < userDirAndFiles.length; i++){ if(userDirAndFiles[i].isDirectory) trace(userDirAndFiles[i].nativePath); //完全なパス名で出力 } /* 出力 C:\Documents and Settings\User\.gem C:\Documents and Settings\User\.nbi C:\Documents and Settings\User\.netbeans C:\Documents and Settings\User\.netbeans-derby C:\Documents and Settings\User\.netbeans-registration C:\Documents and Settings\User\Application Data C:\Documents and Settings\User\Cookies C:\Documents and Settings\User\Favorites C:\Documents and Settings\User\InstallAnywhere C:\Documents and Settings\User\Local Settings C:\Documents and Settings\User\My Documents C:\Documents and Settings\User\NetHood C:\Documents and Settings\User\PrintHood C:\Documents and Settings\User\rails C:\Documents and Settings\User\Recent C:\Documents and Settings\User\SendTo C:\Documents and Settings\User\Templates C:\Documents and Settings\User\Tracing C:\Documents and Settings\User\スタート メニュー C:\Documents and Settings\User\デスクトップ */
ファイルのサイズを調べる
var f1:File = new File("file:///C:\\test.txt"); //"C:\\test.txt"でもよい trace(f1.size + " bytes"); //ファイルサイズを出力 /* 出力 22 bytes */
ファイル、フォルダのコピー
var srcFile:File = File.applicationDirectory.resolvePath("simple.txt"); //Airのインストール先フォルダの中のsimple.txtを指定 var destFile:File = File.desktopDirectory.resolvePath("simple.txt"); //デスクトップのsimple.txtを指定 srcFile.copyTo(destFile, true); //srcFileをdestFileへコピー、第二引数はtrueにすると同じファイル名のものがすでに存在していた場合上書きする、falseの場合はエラーを出す。
ディレクトリの作成とファイルの移動
var src:File = File.desktopDirectory.resolvePath("myTest.txt"); //デスクトップのtest.txtを参照 var tgt:File = File.documentsDirectory.resolvePath("AIR/test.txt"); //ドキュメントフォルダのAIR/test.txtを参照 var tgtParent:File = tgt.parent; //AIR/test.txtの親フォルダ(AIR)を参照。 tgtParent.createDirectory(); //フォルダを作成 src.moveTo(tgt, true); //myTest.txtをAIR/test.txtに移動, 第二引数がtrueの場合はすでに同じ名前のフォルダがあった場合上書きする。falseの場合はエラーを出す
フォルダを削除、ファイル、フォルダをゴミ箱へ移動する
var testDir:File = File.documentsDirectory.resolvePath("testDirectory/"); //ドキュメントフォルダ/testDirectoryへの参照 testDir.createDirectory(); //フォルダ作成 trace(testDir.exists); //存在の確認 testDir.deleteDirectory(); //フォルダの削除 trace(testDir.exists); /*出力 true false */ ||< var file:File = File.desktopDirectory.resolvePath("test2.txt"); if(file.exists) file.moveToTrash(); //ゴミ箱へ移動