用于执行一般的文件系统操作 (reading and writing is done via NSData, et. al.).
主要功能包括:
从一个文件中读取数据;
向一个文件中写入数据;
删除文件;
复制文件;
移动文件;
比较两个文件的内容;
测试文件的存在性;
读取/更改文件的属性… …
Just alloc/init an instance and start performing operations. Thread safe.
NSFileManager *fileManager = [[NSFileManager alloc]init]; //最好不要用defaultManager。 NSData *myData = [fileManager contentsAtPath:path]; // 从一个文件中读取数据 [fileManager createFileAtPath:path contents:myData attributes:dict];//向一个文件中写入数据,属性字典允许你制定要创建 [fileManager removeItemAtPath:path error:err]; [fileManager moveItemAtPath:path toPath:path2 error:err]; [fileManager copyItemAtPath:path toPath:path2 error:err]; [fileManager contentsEqualAtPath:path andPath:path2]; [fileManager fileExistsAtPath:path]; … …
[fileManager currentDirectoryPath]; [fileManager changeCurrentDirectoryPath:path]; [fileManager copyItemAtPath:path toPath:path2 error:err]; [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:err]; [fileManager fileExistsAtPath:path isDirectory:YES]; [fileManager enumeratorAtPath:path]; //获取目录的内容列表。一次可以枚举指定目录中的每个文件。 … … Has a delegate with lots of “should” methods (to do an operation or proceed after an error). And plenty more. Check out the documentation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 -(IBAction ) CreateFile{ NSError *error; NSFileManager *fileMgr = [NSFileManager defaultManager]; NSString *documentsDirectory= [NSHomeDirectory () stringByAppendingPathComponent:@"Documents" ]; [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/myFolder" , NSHomeDirectory ()] attributes:nil ]; NSString *filePath= [documentsDirectory stringByAppendingPathComponent:@"file2.txt" ]; NSString *str= @"iPhoneDeveloper Tips\nhttp://iPhoneDevelopTips,com" ; [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; NSLog (@"Documentsdirectory: %@" ,[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); }
对一个文件重命名,想要重命名一个文件,我们需要把文件移到一个新的路径下。下面的代码创建了我们所期望的目标文件的路径,然后请求移动文件以及在移动之后显示文件目录。
1 2 3 4 5 6 7 8 9 NSString *filePath2= [documentsDirectorystringByAppendingPathComponent:@"file2.txt" ]; if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES )NSLog (@"Unable to move file: %@" , [error localizedDescription]);NSLog (@"Documentsdirectory: %@" ,[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
为了使这个技巧完整,让我们再一起看下如何删除一个文件:
1 2 3 4 5 6 if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES )NSLog (@"Unable to delete file: %@" , [error localizedDescription]);NSLog (@"Documentsdirectory: %@" ,[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
一旦文件被删除了,正如你所预料的那样,文件目录就会被自动清空: 这些示例能教你的,仅仅只是文件处理上的一些皮毛。想要获得更全面、详细的讲解,你就需要掌握NSFileManager文件的知识。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 - (NSString *)attchmentFolder{ NSString *document = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory , NSUserDomainMask , YES ) objectAtIndex:0 ]; NSString *path = [document stringByAppendingPathComponent:@"Attchments" ]; NSFileManager *manager = [NSFileManager defaultManager]; if (![manager contentsOfDirectoryAtPath:path error:nil ]){ [manager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil ]; } return path; }
–清除附件
1 BOOL result = [[NSFileManager defaultManager] removeItemAtPath:[[MOPAppDelegate instance] attchmentFolder] error:nil ];
1 2 3 4 5 6 7 8 9 10 11 12 NSString *filePath = [self dataFilePath];if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) { } -(NSString *)dataFilePath { NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory , NSUserDomainMask , YES ); NSString *documentDirectory = [paths objectAtIndex:0 ]; return [documentDirectory stringByAppendingPathComponent:@"data.plist" ]; }
常用路径工具函数
1 2 3 4 5 NSString * NSUserName (); 返回当前用户的登录名 NSString * NSFullUserName (); 返回当前用户的完整用户名 NSString * NSHomeDirectory (); 返回当前用户主目录的路径 NSString * NSHomeDirectoryForUser (); 返回用户user的主目录 NSString * NSTemporaryDirectory (); 返回可用于创建临时文件的路径目录
常用路径工具方法
1 2 3 4 5 6 7 8 9 10 11 -(NSString *) pathWithComponents:components 根据components(NSArray 对象)中元素构造有效路径 -(NSArray *)pathComponents 析构路径,获取路径的各个部分 -(NSString *)lastPathComponent 提取路径的最后一个组成部分 -(NSString *)pathExtension 路径扩展名 -(NSString *)stringByAppendingPathComponent:path 将path添加到现有路径末尾 -(NSString *)stringByAppendingPathExtension:ext 将拓展名添加的路径最后一个组成部分 -(NSString *)stringByDeletingPathComponent 删除路径的最后一个部分 -(NSString *)stringByDeletingPathExtension 删除路径的最后一个部分 的扩展名 -(NSString *)stringByExpandingTildeInPath 将路径中的代字符扩展成用户主目录(~)或指定用户主目录(~user) -(NSString *)stringByResolvingSymlinksInPath 尝试解析路径中的符号链接 -(NSString *)stringByStandardizingPath 通过尝试解析~、..、.、和符号链接来标准化路径
使用路径NSPathUtilities.h
1 2 3 4 5 6 7 tempdir = NSTemporaryDirectory (); 临时文件的目录名 path = [fm currentDirectoryPath]; [path lastPathComponent]; 从路径中提取最后一个文件名 fullpath = [path stringByAppendingPathComponent:fname];将文件名附加到路劲的末尾 extenson = [fullpath pathExtension]; 路径名的文件扩展名 homedir = NSHomeDirectory ();用户的主目录 component = [homedir pathComponents]; 路径的每个部分
NSProcessInfo类:允许你设置或检索正在运行的应用程序的各种类型信息
1 2 3 4 5 6 7 8 9 10 11 (NSProcessInfo *)processInfo 返回当前进程的信息 -(NSArray *)arguments 以NSString 对象数字的形式返回当前进程的参数 -(NSDictionary *)environment 返回变量/值对词典。描述当前的环境变量 -(int )processIdentity 返回进程标识 -(NSString *)processName 返回进程名称 -(NSString *)globallyUniqueString 每次调用该方法都会返回不同的单值字符串,可以用这个字符串生成单值临时文件名 -(NSString *)hostname 返回主机系统的名称 -(unsigned int )operatingSystem 返回表示操作系统的数字 -(NSString *)operatingSystemName 返回操作系统名称 -(NSString *)operatingSystemVersionString 返回操作系统当前版本 -(void )setProcessName:(NSString *)name 将当前进程名称设置为name
NSFileHandle类允许更有效地使用文件。
可以实现如下功能:
1、打开一个文件,执行读、写或更新(读写)操作;
2、在文件中查找指定位置;
3、从文件中读取特定数目的字节,或将特定数目的字节写入文件中
另外,NSFileHandle类提供的方法也可以用于各种设备或套接字。一般而言,我们处理文件时都要经历以下三个步骤:
1、打开文件,获取一个NSFileHandle对象(以便在后面的I/O操作中引用该文件)。
2、对打开文件执行I/O操作。
3、关闭文件。
1 2 3 4 5 6 7 8 NSFileHandle *fileHandle = [[NSFileHandle alloc]init]; fileHandle = [NSFileHandle fileHandleForReadingAtPath:path]; fileHandle = [NSFileHandle fileHandleForWritingAtPath:path]; fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path]; fileData = [fileHandle availableData]; fileData = [fileHandle readDataToEndOfFile]; [fileHandle writeData:fileData]; [fileHandle closeFile];
注:NSFileHandle类没有提供创建文件的功能,所以必须使用NSFileManager来创建文件
参考:http://blog.csdn.net/zhuzhihai1988/article/details/7904333