A. 目录管理
1 NSFileManager*manager = [NSFileManagerdefaultManager];//单例模式 2 3 // 1.获取文件属性 4 NSString *path = @"/Users/hellovoidworld/desktop/oc/test20141121.txt"; 5 NSFileManager *manager = [NSFileManagerdefaultManager]; // 单例模式 6 NSDictionary *attrDic = [manager attributesOfItemAtPath:path error:nil]; 7 NSLog(@"attrDic: %@", attrDic); 8 9 // 2.获得目录下的文件与子文件目录列表 10 NSString *dirPath = @"/Users/hellovoidworld/desktop/oc"; 11 12 // 只能获取到第一级目录的文件和文件夹(名字) 13 NSArray *subDirArr = [manager contentsOfDirectoryAtPath:dirPath error:nil]; 14 NSLog(@"subDirArr: %@", subDirArr); 15 16 // 包含所有文件、子目录(名字) 17 NSArray *subPath = [manager subpathsAtPath:dirPath]; 18 NSLog(@"subPath: %@", subPath); 19 20 21 // 3.管理目录 22 // 创建目录 23 [manager createDirectoryAtPath:@"/Users/hellovoidworld/desktop/oc/newFolder"withIntermediateDirectories:NOattributes:nilerror:nil]; 24 //withIntermediateDirectories 参数表示要不要创建不存在的所有目录,NO表示只能创建一级目录 25 26 // 移动目录 27 [manager moveItemAtPath:@"/Users/hellovoidworld/desktop/oc/newFolder/existedFolder"toPath:@"/Users/hellovoidworld/desktop/oc/newFolder/movedFolder"error:&error]; 28 // existedFolder会被剪切变成movedFolder,移动到指定位置 29 30 // 删除目录 31 [manager removeItemAtPath:@"/Users/hellovoidworld/desktop/oc/newFolder/deletingFolder"error:nil]; 32 33 // 拷贝文件 34 [manager copyItemAtPath:@"/Users/hellovoidworld/desktop/oc/newFolder/copyingFolder"toPath:@"/Users/hellovoidworld/desktop/oc/newFolder/copiedFolder"error:nil];
B.文件管理
1 NSFileManager *fileManager = [NSFileManager defaultManager]; 2 3 // // 1.获得文件 4 NSString *path = @"/Users/hellovoidworld/desktop/oc/M2.jpg"; 5 NSData *data = [NSData dataWithContentsOfFile:path]; // 提取数据 6 NSLog(@"%ld", data.length); 7 8 NSString *path2 = @"/Users/hellovoidworld/desktop/oc/M2Copy.jpg"; 9 [fileManager createFileAtPath:path2 contents:data attributes:nil]; // 写入数据 10 11 // 2.移动文件,相当于剪切操作 12 NSString *fromPath = @"/Users/hellovoidworld/desktop/oc/M2Copy.jpg"; 13 NSString *toPath = @"/Users/hellovoidworld/desktop/oc/newFolder/M2.jpg"; 14 [fileManager moveItemAtPath:fromPath toPath:toPath error:nil]; 15 16 // 3.删除文件 17 [fileManager removeItemAtPath:@"/Users/hellovoidworld/desktop/oc/newFolder/M2.jpg" error:nil];
C.NSData处理数据
1 // NSData是一个不可变长度的Data类型,可以一次性加载文件内容 2 NSData *data = [NSData dataWithContentsOfFile:@"/Users/hellovoidworld/desktop/oc/newFolder/test.txt"]; 3 NSLog(@"data length = %ld", data.length); 4 5 // 利用NSData写入文件数据 6 [data writeToFile:@"/Users/hellovoidworld/desktop/oc/newFolder/test2.txt" atomically:YES]; 7 8 // NSMutableData 9 NSMutableData *muData = [[NSMutableData alloc] init]; 10 11 NSString *str1 = @"我要好好学习!"; 12 NSString *str2 = @"天天向上!"; 13 NSString *str3 = @"今天休息!"; 14 15 NSDate *data1 = [str1 dataUsingEncoding:NSUTF8StringEncoding]; 16 NSData *data2 = [str2 dataUsingEncoding:NSUTF8StringEncoding]; 17 NSData *data3 = [str3 dataUsingEncoding:NSUTF8StringEncoding]; 18 19 [muData appendData:data1]; 20 [muData appendData:data2]; 21 [muData appendData:data3]; 22 23 NSString *muPath = @"/Users/hellovoidworld/desktop/oc/newFolder/mu.txt"; 24 [muData writeToFile:muPath atomically:YES];