obj-cプログラミング10:Foundationライブラリでのクラスの使用(3)[ファイル管理]


ファイル、ディレクトリ、パス(PATH)管理の内容については、神馬システムにかかわらず避けられないので、Fライブラリでのサポートを見てみましょう.私は簡単に見て、他の方法の命名だけでは少し複雑に見えますが、rubyと比べるとほほほです.
次のコード機能には、ファイルの開く、コピー、移動、コンテンツの取得、フォルダの2つの遍歴方法(再帰と非再帰)があります.
#import <Foundation/Foundation.h>

#define msg(...) NSLog(__VA_ARGS__)

int main(int argc, char *argv[]){
	@autoreleasepool {
		NSString *path = @"data.db";
		NSFileManager *fm;
		NSDictionary *attr;

		fm = [NSFileManager defaultManager];

		//check file is existed
		if([fm fileExistsAtPath: path] == NO){
			NSLog(@"file %@ don't exist!",path);
			return 1;
		}

		//copy to new file ,new_file name must give
		if([fm copyItemAtPath: path toPath: @"new_data.db" error:NULL] == NO){
			NSLog(@"file copy failed!");
			return 2;
		}

		//move file
		if([fm moveItemAtPath:path toPath:@"fixed_data.db" error:NULL] == NO){
			NSLog(@"file rename failed!");
			return 3;
		}

		//cmp 2 file by contents
		if([fm contentsEqualAtPath:@"fixed_data.db" andPath:@"new_data.db"] == NO){
			NSLog(@"files are not Equal!");
		}else{
			NSLog(@"file are Equal!");
		}

		//get file size
		if((attr = [fm attributesOfItemAtPath:@"fixed_data.db" error:NULL]) == nil){
			NSLog(@"can't get file attr!");
		}else{
			msg(@"file size is %llu bytes",\
				[[attr objectForKey: NSFileSize] unsignedLongLongValue]);
		}

		//at last we remove file
		if([fm removeItemAtPath:@"new_data.db" error:NULL] == NO){
			msg(@"file rm failed!");
		}
	}
	return 0;
}

注目すべきは、ファイルのコピーと移動操作について、ターゲットファイルがすでに存在する場合に失敗することである.そして、この2つの操作はターゲットファイル名を与えなければなりませんよ.覚えておいてください.
ファイルの内容を複数回操作する場合は、NSDataクラスをキャッシュとして使用できます.次は別のファイルcopy操作です.
#import <Foundation/Foundation.h>

#define msg(...) NSLog(__VA_ARGS__)

int main(int argc, char *argv[]){
	@autoreleasepool {
		NSFileManager *fm;
		NSData *data;

		fm = [NSFileManager defaultManager];
		data = [fm contentsAtPath:@"data.db"];
		if(data == nil){
			NSLog(@"file read failed!");
			return 1;
		}

		if([fm createFileAtPath:@"cp_data.db" contents:data attributes:nil] == NO){
			NSLog(@"can't copy file");
			return 2;
		}
	}
	return 0;
}

ディレクトリの遍歴にはenumeratorAtPathまたはcontentsofDirectory AtPath:error:メソッド(名前を見て...)を使用します.前者はサブフォルダ内のファイルに深く入り込み、後者はできませんが、再帰プロセスを動的にブロックすることもできます.次のコードでは、最終的にどのようにするかを見ることができます.
#import <Foundation/Foundation.h>

#define msg(...) NSLog(__VA_ARGS__)

int main(int argc, char *argv[]){
	@autoreleasepool {
		NSString *path;
		NSFileManager *fm;
		NSDirectoryEnumerator *dir_er;
		NSArray *files;

		fm = [NSFileManager defaultManager];
		path = [fm currentDirectoryPath];

		dir_er = [fm enumeratorAtPath:path];
		msg(@"PART1 : file list in %@:",path);
		while((path = [dir_er nextObject]) != nil)
			msg(@"%@",path);

		path = [fm currentDirectoryPath];
		files = [fm contentsOfDirectoryAtPath:path error:NULL];
		msg(@"PART2 : all files in path :%@",files);

		BOOL flag = NO;
		dir_er = [fm enumeratorAtPath:path];
		msg(@"PART3 : file list in %@:",path);
		while((path = [dir_er nextObject]) != nil){
			msg(@"%@",path);
			[fm fileExistsAtPath:path isDirectory:&flag];
			if(flag == YES) [dir_er skipDescendents];
		}
	}
	return 0;
}

最初の列挙器dir_に注意Er列挙が最後に戻った後、もう一度列挙すると直接戻ってきます.列挙するオブジェクトはもうありません.他の言語のrewindのような方法は見つかりません.dir_にやり直すしかありません.Er賦値鳥実行結果は次のとおりです.
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./f
2014-07-02 13:41:03.220 f[12245] PART1 : file list in /home/wisy/src/objc_src:
2014-07-02 13:41:03.222 f[12245] a
2014-07-02 13:41:03.223 f[12245] class_std
2014-07-02 13:41:03.223 f[12245] class_std/Box.m
2014-07-02 13:41:03.223 f[12245] class_std/test.m
2014-07-02 13:41:03.223 f[12245] class_std/cls_test
2014-07-02 13:41:03.223 f[12245] class_std/Box.h
2014-07-02 13:41:03.223 f[12245] class_std/cls_test.d
2014-07-02 13:41:03.223 f[12245] cp_data.db
2014-07-02 13:41:03.223 f[12245] t
2014-07-02 13:41:03.223 f[12245] dzh.m
2014-07-02 13:41:03.223 f[12245] var.m
2014-07-02 13:41:03.223 f[12245] t.d
2014-07-02 13:41:03.223 f[12245] class.m
2014-07-02 13:41:03.223 f[12245] f.m
2014-07-02 13:41:03.223 f[12245] a.d
2014-07-02 13:41:03.224 f[12245] f
2014-07-02 13:41:03.224 f[12245] normal.m
2014-07-02 13:41:03.224 f[12245] data.db
2014-07-02 13:41:03.224 f[12245] a.m
2014-07-02 13:41:03.224 f[12245] f.d
2014-07-02 13:41:03.224 f[12245] PART2 : all files in path :(a, "class_std", "cp_data.db", t, "dzh.m", "var.m", "t.d", "class.m", "f.m", "a.d", f, "normal.m", "data.db", "a.m", "f.d")
2014-07-02 13:41:03.224 f[12245] PART3 : file list in /home/wisy/src/objc_src:
2014-07-02 13:41:03.224 f[12245] a
2014-07-02 13:41:03.224 f[12245] class_std
2014-07-02 13:41:03.224 f[12245] cp_data.db
2014-07-02 13:41:03.224 f[12245] t
2014-07-02 13:41:03.224 f[12245] dzh.m
2014-07-02 13:41:03.224 f[12245] var.m
2014-07-02 13:41:03.224 f[12245] t.d
2014-07-02 13:41:03.224 f[12245] class.m
2014-07-02 13:41:03.224 f[12245] f.m
2014-07-02 13:41:03.225 f[12245] a.d
2014-07-02 13:41:03.225 f[12245] f
2014-07-02 13:41:03.225 f[12245] normal.m
2014-07-02 13:41:03.225 f[12245] data.db
2014-07-02 13:41:03.225 f[12245] a.m
2014-07-02 13:41:03.225 f[12245] f.d

続きを待つ