// // main.m /* 简单测试NSFileManager类的使用: An NSFileManager object lets you examine the contents of the file system and make changes to it. A file manager object is usually your first interaction with the file system. You use it to locate, create, copy, and move files and directories. You also use it to get information about a file or directory or change some of its attributes. */ #import <Foundation/Foundation.h> /* 使用pwd获取全路径 */ #define PATH @"/Users/apple/Documents/ios_dev/test_case/TestFileManager" int main(int argc, const char * argv[]) { @autoreleasepool { /* defaultManager:返回一个单例,Returns the shared file manager object for the process. */ NSFileManager *fm = [NSFileManager defaultManager]; /* 文件夹下的文件遍历 */ /* contentsOfDirectoryAtPath:Performs a shallow search of the specified directory and returns the paths of any contained items. */ NSError *err1; NSArray *arr1 = [fm contentsOfDirectoryAtPath:PATH error:&err1]; NSLog(@"files in %@ include:%@", PATH, arr1); /* 子文件夹下的文件遍历 */ /* subpathsOfDirectoryAtPath:Performs a deep enumeration of the specified directory and returns the paths of all of the contained subdirectories. */ NSError *err2; NSString *str2 = [NSString stringWithFormat:@"%@/TestFileManager", PATH]; NSArray *arr2 = [fm subpathsOfDirectoryAtPath:str2 error:&err2]; NSLog(@"files in %@ include:%@", str2, arr2); /* 指定位置创建文件夹TestPath */ /* createDirectoryAtPath:Creates a directory with given attributes at the specified path. */ NSError *err3; NSString *str3 = [NSString stringWithFormat:@"%@/TestFileManager/TestPath", PATH]; BOOL ret3 = [fm createDirectoryAtPath:str3 withIntermediateDirectories:YES attributes:nil error:&err3]; NSLog(@"ret3:%d", ret3); /* 在已有的指定路径新建文件TestFile */ /* createFileAtPath:Creates a file with the specified content and attributes at the given location. */ NSString *str4 = [NSString stringWithFormat:@"%@/TestFileManager/TestPath/newFile", PATH]; NSString *strFileContents = @"this is my fitst text file."; NSData *data4 = [strFileContents dataUsingEncoding:NSUTF8StringEncoding]; BOOL ret4 = [fm createFileAtPath:str4 contents:data4 attributes:nil]; NSLog(@"ret4:%d", ret4); /* 删除指定路径下的文件或文件夹 */ /* removeItemAtPath:Removes the file or directory at the specified path. */ NSError *err5; NSString *str5 = [NSString stringWithFormat:@"%@/TestFileManager/TestPath", PATH]; BOOL ret5 = [fm removeItemAtPath:str5 error:&err5]; NSLog(@"ret5:%d", ret5); } return 0; }
输出结果:
2015-12-07 22:56:47.540 TestFileManager[667:33461] files in /Users/apple/Documents/ios_dev/test_case/TestFileManager include:( ".DS_Store", TestFileManager, "TestFileManager.xcodeproj" ) 2015-12-07 22:56:47.541 TestFileManager[667:33461] files in /Users/apple/Documents/ios_dev/test_case/TestFileManager/TestFileManager include:( ".DS_Store", "main.m", TestPath, "TestPath/newFile" ) 2015-12-07 22:56:47.541 TestFileManager[667:33461] ret3:1 2015-12-07 22:56:47.543 TestFileManager[667:33461] ret4:1 2015-12-07 22:56:47.543 TestFileManager[667:33461] ret5:1其中,ret4执行后,文件夹路径显示:
ret5执行后,文件夹路径显示:
</pre><pre name="code" class="objc">