首页 > 代码库 > iOS NSFileManager文件操作
iOS NSFileManager文件操作
参考:http://blog.csdn.net/xyz_lmn/article/details/8968213 感谢这些先行者
文件操作常用操作记录一下:
//获取沙盒的根路径 NSString *dirHome = NSHomeDirectory(); NSLog(@"%@",dirHome); //获取Documents目录路径 NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [path objectAtIndex:0]; NSLog(@"%@",documentsDirectory); //获取Library 目录路径 path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [path objectAtIndex:0]; NSLog(@"%@",libraryDirectory); //获取caches目录路径 path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *caches = [path objectAtIndex:0]; NSLog(@"%@",caches); //获取Tmp 目录路径 NSString *tmpDirectory = NSTemporaryDirectory(); NSLog(@"%@",tmpDirectory); //创建文件夹 //1 获得文件路径 //2 创建文件管理器 //3 用文件管理器在目录上创建文件 path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"]; NSFileManager *fm = [NSFileManager defaultManager]; BOOL res = [fm createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil]; if(!res) { NSLog(@"文件夹创建成功"); } else { NSLog(@"文件夹创建失败"); } //创建文件(与文件夹的创建相同) path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"text"]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"123.txt"]; fm = [NSFileManager defaultManager]; res = [fm createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil]; if(res) { NSLog(@"文件夹创建成功"); } else { NSLog(@"文件夹创建失败"); } //写数据到文件 path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"text"]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"123.txt"]; NSString *content = @"文件写入的内容"; res = [content writeToFile:documentsDirectory atomically:YES encoding:NSUTF8StringEncoding error:nil]; if(res) { NSLog(@"文件夹创建成功"); } else { NSLog(@"文件夹创建失败"); } //读文件数据 path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"text"]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"123.txt"]; content = [NSString stringWithContentsOfFile:documentsDirectory encoding:NSUTF8StringEncoding error:nil]; NSLog(@"文件读取成功:%@",content); //文件的属性 path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); documentsDirectory = [path objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"text"]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"123.txt"]; fm = [NSFileManager defaultManager]; NSDictionary *fileAttributes = [fm attributesOfItemAtPath:documentsDirectory error:nil]; NSArray *keys; id key,value; keys = [fileAttributes allKeys]; int count = [keys count]; for (int i= 0; i<count; i++) { key = [keys objectAtIndex:i]; value = http://www.mamicode.com/[fileAttributes objectForKey:key];>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。