好吧,不管神马系统都无可避免的要说到文件,目录,路径(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

待续

obj-c编程10:Foundation库中类的使用(3)[文件管理]的更多相关文章

  1. obj-c编程10:Foundation库中类的使用(4)[文件管理,查询当前进程信息]

    接上一篇文件管理博文.我们可以用NSPathUtilities.h中包含的NSString函数和分类扩展来以兼容的方式处理路径.下面凡是有系统编程经验的童鞋都知道是啥意思了: #import < ...

  2. obj-c编程10:Foundation库中类的使用(2)[字符串,数组]

    Foundation库的内容不可谓不多,就算很精简的说篇幅也受不了啊!笨猫一向反对博客文章一下子拖拖拉拉写一大坨!KISS哦!so将上一篇文章再分一篇来说,于是有了这篇,可能还会有(3)哦... 我发 ...

  3. obj-c编程10:Foundation库中类的使用(6)[线程和操作队列]

    任何语言都不能避而不谈线程这个东东,虽然他是和平台相关的鸟,虽说unix哲学比较讨厌线程的说...线程不是万能灵药,但有些场合还是需要的.谈到线程就不得不考虑同步和死锁问题,见如下代码: #impor ...

  4. obj-c编程10:Foundation库中类的使用(5)[时间对象]

    隔了好久才有了这新的一篇,还是无奈的时间啊!so这次我们就着重谈谈它喽. F库中有很多时间相关的类,比如NSDate,NSTimeInterval,NSTimeZone,NSDateComponent ...

  5. obj-c编程10:Foundation库中类的使用(1)[数字,字符串]

    我们知道在mac或iphone上编程最终逃不开os x平台,你无法在windows或linux上开发纯正的apple程序.(so不要舍不得银子买mac啦)虽说linux和windows上有移植的obj ...

  6. C++的XML编程经验――LIBXML2库使用指南[转]

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  7. Java 库:为 Java 程序员而生的 10 + 最佳库

    众所周知,Java 的生态环境相当庞大,包含了数量相当可观的官方及第三方库.利用这些库,可以解决在用 Java 开发时遇到的各类问题,让开发效率得到显著提升. 举些例子,最常用的官方库有 java.l ...

  8. C++的XML编程经验――LIBXML2库使用指南

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  9. 并发编程 10—— 任务取消 之 关闭 ExecutorService

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

随机推荐

  1. Android开发 Jar mismatch! Fix your dependencies的问题

    有时候,当我们在导入Library的时候,会遇到Jar mismatch! Fix your dependencies这个错误.可能有如下原因: 1.两个项目的android-support-v4.j ...

  2. 关于Lt分发系统的时序图分析

    我们已经知道,系统共分为两个模块,mather与son 同时系统允许的操作也有三种,向mather提交war包,我某个服务器更新代码,为所有服务器更新代码 我们一个一个来看 先说,向mather提交w ...

  3. Oracle启用scott用户

    先查询一下目前数据库是否有scott用户 select username,account_status from dba_users where username like '%SCOTT%'; 如果 ...

  4. python的subprocess:子程序调用(调用执行其他命令);获取子程序脚本当前路径问题

    python当前进程可以调用子进程,子进程可以执行其他命令,如shell,python,java,c... 而调用子进程方法有 os模块 参见:http://blog.csdn.net/longshe ...

  5. 【Android 应用开发】 Application 使用分析

    博客地址 : http://blog.csdn.net/shulianghan/article/details/40737419 代码下载 : Android 应用 Application 经典用法; ...

  6. java设计模式---合成模式2

    合成模式属于对象的结构模式,有时又叫做"部分--整体"模式.合成模式将对象组织到树结构中,可以用来描述整体与部分的关系.合成模式可以使客户端将单纯元素与复合元素同等看待. 合成模式 ...

  7. Cocos2D旋转炮塔到指定角度(一)

    原文地址:Rotating Turrets: How To Make A Simple iPhone Game with Cocos2D 2.X Part 2 翻译有节选和删除. 在你旋转炮塔之前,首 ...

  8. 【翻译】Sencha Cmd中脚本压缩方法之比较

    概述 为什么要修改默认设置 YUI压缩 Google Closure编译器 UglifyJS 案例研究Ext JS 6示例应用程序 注意事项 自定义JS压缩 小结 概述 这么多年来,Web开发人员都被 ...

  9. PO标准form的一点疑问

    最近在修改采购订单form的时候,发现采购订单form往数据库中插数据的地方找不到,程序太多.我们又需要根据界面上的item的值在订单界面数据生成数据库数据时插值时,只能是想其他办法,一种是在on-i ...

  10. 【leetcode74】Sum of Two Integers(不用+,-求两数之和)

    题目描述: 不用+,-求两个数的和 原文描述: Calculate the sum of two integers a and b, but you are not allowed to use th ...