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;
}

值得注意的是对于文件拷贝和移动操作,如果目标文件已存在就会失败.而且这两种操作都要给出目标文件名哦,切记!

对于多次操作文件内容的情况我们可以用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或者contentsOfDirectoryAtPath: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

待续

上一篇:CCF-201403-1-相反数


下一篇:Visual studio 2015程序转Eclipse gun编译出现的问题总结