【NSFileManager】ディレクトリの管理
iosではアプリケーションごとにディレクトリを管理できる
□ディレクトリ構造
Application_Home/Documents
Application_Home/Library/Preferences
Application_Home/Library/Caches
Application_Home/tmp
上記ディレクトリのうち、Preferencesは直接アクセスしてはだめで、tmp配下においても自動でクリーンアップされないため、実際に利用するのはDocumentsとCachesの2つ
ただし、Documentsを使うと審査でリジェクトされるので、Cachesが無難
ーーーーーーーーーーーーーーーーーーーーーーー
■ディレクトリの作成
Application_Home/Documents 直下にdogフォルダを作成
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError *error;
// ホームディレクトリを取得
NSString *homeDir = NSHomeDirectory();
// 作成するディレクトリのパスを指定(利用するのはホームディレクトリのDocuments or Caches)
NSString *dirPath3 = [homeDir stringByAppendingPathComponent:@"Documents/dog/"];
// ディレクトリを作成
BOOL result = [fileManager createDirectoryAtPath:dirPath3
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (result) {
NSLog(@"ディレクトリの作成に成功:%@", dirPath3);
} else {
NSLog(@"ディレクトリの作成に失敗:%@", error.description);
}
ーーーーーーーーーーーーーーーーーーーーーーー
■ファイルのコピー
Application_Home/Documents/dog/picture.jpg を
Application_Home/Documents/cat へコピー
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError *error;
//コピー元のパス(Documentsフォルダ)
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
//ホームディレクトリ/Documents/dog/picture.jpgのパス
NSString *filePath = [NSString stringWithFormat:@"%@/dog/picture.jpg", docDirectory];
//コピー先のパス
NSString *dirPath = [homeDir stringByAppendingPathComponent:@"Documents/cat/picture.jpg"];
//コピー
BOOL result =[fileManager copyItemAtPath:filePath toPath:dirPath error:nil];
if (result) {
NSLog(@"コピーに成功");
} else {
NSLog(@"コピーに失敗");
}
ーーーーーーーーーーーーーーーーーーーーーーー
■ディレクトリの一覧取得
Application_Home/Documents/cat フォルダのファイル一覧を取得
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError *error;
// ホームディレクトリを取得
NSString *homeDir = NSHomeDirectory();
// パスを指定
NSString *dirPath3 = [homeDir stringByAppendingPathComponent:@"Documents/cat/"];
//パスをlistに入れてログで出力
NSArray *list = [fileManager contentsOfDirectoryAtPath:dirPath3 error:&error];
for (NSString *path in list) {
NSLog(@"catフォルダ:%@", path);
}
ーーーーーーーーーーーーーーーーーーーーーーー
■ファイル削除
Application_Home/Documents/dog/picture.jpg を削除
NSError *error = nil;
NSString *homeDir = NSHomeDirectory();
NSString *filepath = [homeDir stringByAppendingPathComponent:@"Documents/dog/picture.jpg"];
NSURL *pathurl = [NSURL fileURLWithPath:filepath];
//ファイル削除
BOOL result=[[NSFileManager defaultManager] removeItemAtURL:pathurl error:&error];
if (result) {
NSLog(@"削除に成功");
} else {
NSLog(@"削除に失敗");
}
●参考
http://d.hatena.ne.jp/ntaku/20110104/1294146555
□ディレクトリ構造
Application_Home/Documents
Application_Home/Library/Preferences
Application_Home/Library/Caches
Application_Home/tmp
上記ディレクトリのうち、Preferencesは直接アクセスしてはだめで、tmp配下においても自動でクリーンアップされないため、実際に利用するのはDocumentsとCachesの2つ
ただし、Documentsを使うと審査でリジェクトされるので、Cachesが無難
ーーーーーーーーーーーーーーーーーーーーーーー
■ディレクトリの作成
Application_Home/Documents 直下にdogフォルダを作成
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError *error;
// ホームディレクトリを取得
NSString *homeDir = NSHomeDirectory();
// 作成するディレクトリのパスを指定(利用するのはホームディレクトリのDocuments or Caches)
NSString *dirPath3 = [homeDir stringByAppendingPathComponent:@"Documents/dog/"];
// ディレクトリを作成
BOOL result = [fileManager createDirectoryAtPath:dirPath3
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (result) {
NSLog(@"ディレクトリの作成に成功:%@", dirPath3);
} else {
NSLog(@"ディレクトリの作成に失敗:%@", error.description);
}
ーーーーーーーーーーーーーーーーーーーーーーー
■ファイルのコピー
Application_Home/Documents/dog/picture.jpg を
Application_Home/Documents/cat へコピー
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError *error;
//コピー元のパス(Documentsフォルダ)
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
//ホームディレクトリ/Documents/dog/picture.jpgのパス
NSString *filePath = [NSString stringWithFormat:@"%@/dog/picture.jpg", docDirectory];
//コピー先のパス
NSString *dirPath = [homeDir stringByAppendingPathComponent:@"Documents/cat/picture.jpg"];
//コピー
BOOL result =[fileManager copyItemAtPath:filePath toPath:dirPath error:nil];
if (result) {
NSLog(@"コピーに成功");
} else {
NSLog(@"コピーに失敗");
}
ーーーーーーーーーーーーーーーーーーーーーーー
■ディレクトリの一覧取得
Application_Home/Documents/cat フォルダのファイル一覧を取得
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError *error;
// ホームディレクトリを取得
NSString *homeDir = NSHomeDirectory();
// パスを指定
NSString *dirPath3 = [homeDir stringByAppendingPathComponent:@"Documents/cat/"];
//パスをlistに入れてログで出力
NSArray *list = [fileManager contentsOfDirectoryAtPath:dirPath3 error:&error];
for (NSString *path in list) {
NSLog(@"catフォルダ:%@", path);
}
ーーーーーーーーーーーーーーーーーーーーーーー
■ファイル削除
Application_Home/Documents/dog/picture.jpg を削除
NSError *error = nil;
NSString *homeDir = NSHomeDirectory();
NSString *filepath = [homeDir stringByAppendingPathComponent:@"Documents/dog/picture.jpg"];
NSURL *pathurl = [NSURL fileURLWithPath:filepath];
//ファイル削除
BOOL result=[[NSFileManager defaultManager] removeItemAtURL:pathurl error:&error];
if (result) {
NSLog(@"削除に成功");
} else {
NSLog(@"削除に失敗");
}
●参考
http://d.hatena.ne.jp/ntaku/20110104/1294146555
2013-11-19 22:43
nice!(0)
コメント(0)
トラックバック(0)
コメント 0