文件/文件夹管理
注:以下直接用代码示例说明
一、NSFileManager运用
1 #pragma mark 使用NSFileManager进行文件操作 2 - (void)fileCheckedByFileManager 3 { 4 NSString *fileName = @"testFile"; 5 NSFileManager *fm = [NSFileManager defaultManager]; 6 // 当前目录移到程序的temp目录 7 [fm changeCurrentDirectoryPath:NSTemporaryDirectory()]; 8 9 NSLog(@"fileManager path = %@", [fm currentDirectoryPath]); 10 11 // 判断testFile文件是否存在,不存在则创建 12 if (![fm fileExistsAtPath:fileName]) { 13 NSLog(@"file is no exist"); 14 15 NSString *data = @"爱疯SD卡减肥iodfslk地方分解护发那个房间给你熬到开始拉法基"; 16 if ([fm createFileAtPath:fileName contents:[data dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]) { 17 NSLog(@"file creat succeed"); 18 } 19 else 20 return; 21 } 22 23 // 拷贝testFile 24 NSString *copyFileName = @"copyFile"; 25 if (![fm copyItemAtPath:fileName toPath:copyFileName error:NULL]) { 26 NSLog(@"copy file %@ to %@ error!", fileName, copyFileName); 27 } 28 29 // 比对testFile与拷贝的文件是否一致 30 if ([fm contentsEqualAtPath:fileName andPath:copyFileName]) { 31 NSLog(@"%@ isEqual to %@", fileName, copyFileName); 32 } 33 34 // 重命名或移动文件 35 NSString *moveFileName = @"movedCopyFile"; 36 if ([fm moveItemAtPath:copyFileName toPath:moveFileName error:NULL]) { 37 NSLog(@"move %@ to %@", copyFileName, moveFileName); 38 } 39 40 // 获取文件属性 41 NSDictionary *attr = [fm attributesOfItemAtPath:moveFileName error:NULL]; 42 NSLog(@"%@ fileSize: %llu", moveFileName, [[attr objectForKey:NSFileSize] unsignedLongLongValue]); 43 44 // 显示文件内容 45 NSLog(@"the file contents: %@", [NSString stringWithContentsOfFile:moveFileName encoding:NSUTF8StringEncoding error:NULL]); 46 47 // 删除文件 48 if ([fm removeItemAtPath:fileName error:NULL] && [fm removeItemAtPath:moveFileName error:NULL] && [fm removeItemAtPath:outFileName error:NULL]) 49 NSLog(@"file %@ and %@ and %@ delete succeed!", fileName, moveFileName, outFileName); 50 }
PS:再加上个文件夹内容的枚举
1 #pragma mark 枚举文件夹内容 2 - (void)enumeratorDir 3 { 4 NSFileManager *fm = [NSFileManager defaultManager]; 5 // [fm changeCurrentDirectoryPath:NSHomeDirectory()]; 6 7 // 枚举指定目录所有内容 8 NSEnumerator *dirEnum = [fm enumeratorAtPath:NSHomeDirectory()]; 9 if (!dirEnum) return; 10 11 NSString *path; 12 while ((path = dirEnum.nextObject)) { 13 // 提取路径的最后一项 以及 后缀 14 NSLog(@"lastPathComponent:%@ extentsion:%@", [path lastPathComponent], [path pathExtension]); 15 } 16 17 NSLog(@"-----------------------------------------------------------------------------------"); 18 19 // 枚举指定目录下一级内容 20 NSArray *dirArray = [fm contentsOfDirectoryAtPath:NSHomeDirectory() error:NULL]; 21 for (NSString *path in dirArray) { 22 // NSLog(@"%@", path); 23 // 打印全路径 24 NSLog(@"%@", [NSHomeDirectory() stringByAppendingPathComponent:path]); 25 } 26 27 NSLog(@"-----------------------------------------------------------------------------------"); 28 29 // 获取指定目录组成 30 NSArray *components = [NSHomeDirectory() pathComponents]; 31 for (NSString *path in components) { 32 NSLog(@"%@", path); 33 } 34 }
二、NSFileHandle
1 // 使用NSFileHandle读写文件 2 NSFileHandle *inFile = [NSFileHandle fileHandleForReadingAtPath:fileName]; 3 NSString *outFileName = @"outFileByHandle"; 4 if ([fm createFileAtPath:outFileName contents:nil attributes:nil]) { 5 NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:outFileName]; 6 if (outFile) { 7 NSLog(@"fileSize:%llu", [outFile seekToEndOfFile]); 8 } 9 [outFile truncateFileAtOffset:0]; // 因为它可能包含数据,截断输出文件 10 11 // 从inFile中读取数据,将它写到outFile 12 NSData *buffer = [inFile readDataToEndOfFile]; 13 [outFile writeData:buffer]; 14 15 [outFile closeFile]; 16 17 NSLog(@"NSFileHandle contents: %@", [NSString stringWithContentsOfFile:outFileName encoding:NSUTF8StringEncoding error:NULL]); 18 } 19 [inFile closeFile];