OBjective-C:文件管理类NSFileManager
文件管理类NSFileManager类:对文件进行创建、复制、重命名、删除等,一般不对文件内容进行操作。 NSData类和NSMutableData类:相当于数据缓冲区 NSFileManager是一个单例类,可以通过defalutManager类方法创建 + (NSFileManager *)defaultManager;//通过这个方法创建的对象始终是同一个 一些文件操作的概念: -当前目录:. -上级目录:.. -根目录:/ -home目录:~ -绝对路径:从根目录开始的路经 -相对路径:从当前目录开始的路径 方法 描述 -(NSData) contentsAtPath:*path 从一个文件读取数据 -(BOOL) createFileAtPath:pathcontents: (NSData)*dataattributes:attr 向一个文件写入数据 -(`BOOL) removeItemAtPath:patherror:err 删除一个文件 -(BOOL) moveItemAtPath:fromtoPath:toerror:err 重命名或移动一个文件(to不能是已存在的) -(BOOL) copyItemAtPath:fromtoPath:toerror:err 复制一个文件(to不能是已存在的) -(BOOL) contentsEqualAtPath:path1andPath:path2 比较这两个文件的内容 -(BOOL) fileExistsAtPath:path 测试文件是否存在 -(BOOL) isReadableFileAtPath:path 测试文件是否存在且是可读文件 -(BOOL) isWritableFileAtPath:path 测试文件是否存在且是可写文件 -(NSDictionary) attributesOfItemAtPath:*patherror:err 获取文件的属性 -(BOOL) setAttributesOfItemAtPath:attrerror:err 更改文件的属性 —————-目录—————- —————目录—————- -(NSString *) currentDirectoryPath 获取当前目录 -(BOOL) changeCurrentDirectoryPath:path 更改当前目录 -(BOOL) copyItemAtPath:fromtoPath:toerror:err 复制目录结构(to不能是已存在的) -(BOOL) createDirectoryAtPath:pathwithIntermediateDirectories: (BOOL)flagattributes:attr 创建一个新目录 -(NSArray) contentsOfDirectoryAtPath:*patherror:err 列出目录内容 -(NSDirectoryEnumerator) enumeratorAtPath:*path 枚举目录的内容 -(BOOL) removeItemAtPath:patherror:err 删除空目录 -(BOOL) moveItemAtPath:fromtoPath:toerror:err 重命名或移动一个目录(to不能是已存在的) 1、 创建NSFileManager对象,其实都是同一个对象 1 NSFileManager *fm = [NSFileManager defaultManager]; 2 NSFileManager *fm2 = [NSFileManager defaultManager]; 3 4 NSLog(@"%@,%@",fm,fm2); 2015-08-24 20:28:23.744 01-NSFlleManager[861:32223] <NSFileManager: 0x100306940>,<NSFileManager: 0x100306940> 2、获取当前路径 1 NSString *currentPath = [fm currentDirectoryPath]; 2 NSLog(@"当前目录:%@0",currentPath); 2015-08-24 20:29:15.624 01-NSFlleManager[869:32586] 当前目录:/Users/mac/Library/Developer/Xcode/DerivedData/01-NSFlleManager-bjtgjiggpfdimgemqrewbfyjlwwu/Build/Products/Debug0 Program ended with exit code: 0 3、改变当前路径 1 [fm changeCurrentDirectoryPath:@".."]; 2 currentPath = [fm currentDirectoryPath]; 3 NSLog(@"当前目录:%@0",currentPath); 2015-08-24 20:30:54.837 01-NSFlleManager[878:33188] 当前目录:/Users/mac/Library/Developer/Xcode/DerivedData/01-NSFlleManager-bjtgjiggpfdimgemqrewbfyjlwwu/Build/Products0 Program ended with exit code: 0 4.创建目录 1 NSError *error; 2 [fm createDirectoryAtPath:@"temp" withIntermediateDirectories:YES attributes:nil error:&error]; 3 if(error) 4 { 5 NSLog(@"创建目录错误:%@",error); 6 } 7 else 8 { 9 NSLog(@"创建成功"); 10 } 11 12 [fm changeCurrentDirectoryPath:@"temp"]; 13 currentPath = [fm currentDirectoryPath]; 14 NSLog(@"当前目录:%@0",currentPath); 2015-08-24 20:32:48.230 01-NSFlleManager[886:33837] 创建成功 2015-08-24 20:32:48.230 01-NSFlleManager[886:33837] 当前目录:/Users/mac/Library/Developer/Xcode/DerivedData/01-NSFlleManager-bjtgjiggpfdimgemqrewbfyjlwwu/Build/Products/temp0 Program ended with exit code: 0 5.复制文件(目录) 1 [fm copyItemAtPath:@"../Debug/01-NSFlleManager" toPath:@"./02-NSFlleManager" error:&error]; 2 if(error) 3 { 4 NSLog(@"复制文件失败:%@",error); 5 } 6 else 7 { 8 NSLog(@"复制文件成功"); 9 } 6.移动文件(目录),在同一个目录下改名操作 1 [fm moveItemAtPath:@"02-NSFlleManager" toPath:@"03-NSFlleManager" error:&error]; 2 if(error) 3 { 4 NSLog(@"文件移动失败:%@",error); 5 } 6 else 7 { 8 NSLog(@"文件移动成功"); 9 } 7.删除文件(目录) 1 error = nil; 2 [fm removeItemAtPath:@"../01-NSFlleManager" error:&error]; 3 if(error) 4 { 5 NSLog(@"文件删除失败:%@",error); 6 } 7 else 8 { 9 NSLog(@"文件删除成功"); 10 } 8.枚举目录内容 8.1 递归输出所有目录的内容 1 NSDirectoryEnumerator *dirEnumerator = [fm enumeratorAtPath:@"/Users/mac/IOS/OC"]; 2 NSString *path; 3 while(path = [dirEnumerator nextObject]) 4 { 5 NSLog(@"%@",path); 6 } 8.2 .只输出当前目录的内容 1 NSArray *files = [fm contentsOfDirectoryAtPath:@"/Users/mac/IOS/OC" error:&error]; 2 NSLog(@"%@",files); 9.列出目录(文件)的属性 1 NSDictionary *attr = [fm attributesOfItemAtPath:@"/Users/mac/IOS/OC" error:&error]; 2 NSLog(@"%@",attr); 2015-08-24 20:40:56.755 01-NSFlleManager[912:36354] { NSFileCreationDate = "2015-08-10 01:39:40 +0000"; NSFileExtensionHidden = 0; NSFileGroupOwnerAccountID = 20; NSFileGroupOwnerAccountName = staff; NSFileModificationDate = "2015-08-24 01:22:42 +0000"; NSFileOwnerAccountID = 501; NSFileOwnerAccountName = mac; NSFilePosixPermissions = 493; NSFileReferenceCount = 13; NSFileSize = 442; NSFileSystemFileNumber = 1685728; NSFileSystemNumber = 16777218; NSFileType = NSFileTypeDirectory; } Program ended with exit code: 0 10.创建文件 1 NSString *str = @"this is a test"; 2 NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; 3 if([fm createFileAtPath:@"1.txt" contents:data attributes:nil]) 4 { 5 NSLog(@"创建文件成功"); 6 } 7 else 8 { 9 NSLog(@"创建文件失败"); 10 } 11.判断文件是否存在 1 if([fm fileExistsAtPath:@"1.txt" isDirectory:NULL]) 2 { 3 NSLog(@"文件存在"); 4 } 5 else 6 { 7 NSLog(@"文件不存在"); 8 } 12.读取文件内容 1 NSData *data2 = [fm contentsAtPath:@"1.txt"]; 2 NSLog(@"%ld",[data2 length]); 3 NSString *str2 = [[NSString alloc]initWithData:data2 encoding:NSUTF8StringEncoding]; 4 NSLog(@"%@",str2); 2015-08-24 20:44:33.660 01-NSFlleManager[920:37495] this is a test Program ended with exit code: 0 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4755793.html,如需转载请自行联系原作者