NSFileManager 文件管理类

文件管理:增。删。移动。复制

//NSFileManager文件管理类,获得文件管理对象的方法

NSFileManager *manager = [NSFileManager defaultManager];

//判断文件是否存在路径下

NSString *filePath = [self getFilePath];

if([manager fileExistsAtPath:filePath]) {

//文件存在

NSLog(@"Exist");

NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];

NSLog(@"%@",arr);

//删除文件的方法

NSString *str = [manager removeItemAtPath:filePath error:nil] ?@"删除成功":@"删除失败";

NSLog(@"%@",str);

}else{

//文件不存在写入

NSLog(@"UnExist");

NSArray * array = @[@"1",@"2",@"3"];

[array writeToFile:filePath atomically:YES];

}

}

- (void)viewDidLoad {

[super viewDidLoad];

[self getFilePath];

NSArray * array = @[@"1"];

[array writeToFile:[self getFilePath] atomically:YES];

NSFileManager *manager = [NSFileManager defaultManager];

//可以判断文件是否存在

//[manager fileExistsAtPath:<#(nonnull NSString *)#>];

NSError *error;

//可以创建文件夹

//createDirectoryAtPath要创建的文件路径以及文件夹的名字

//withIntermediateDirectories :当上一级目录不存在的时候,直接创建上一级目录

//error错误信息参数类型:双指针

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"a"];

[manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];

NSString *path1 = [path stringByAppendingPathComponent:@"a1"];

[manager createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nilerror:&error];

NSString *str =@"本地存储";

NSString *filePath = [path1 stringByAppendingString:@"/string.plist"];

[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

//移动包含了移动并且重命名的过程

NSString *sourcePath = [NSHomeDirectory() stringByAppendingPathComponent:@"a/a1"];

//对于目的路径移动的是文件的话-----文件的名字

//移动的是文件夹的话------要在路径下有一个文件夹

NSString *destination = [NSHomeDirectory() stringByAppendingPathComponent:@"a/a111"];

[manage rmoveItemAtPath:sourcePathtoPath:destination error:&error];

//copy

NSString *pathDes = [NSHomeDirectory() stringByAppendingPathComponent:@"aaaaaa"];

[manager copyItemAtPath:destination toPath:pathDes error:&error];

//删除&error双指针指向

[manager removeItemAtPath:destination error:&error];

}

- (NSString *)getFilePath {

NSString *homeFilePath =NSHomeDirectory();

//不可以直接创建文件夹

NSString *filePath = [homeFilePath stringByAppendingPathComponent:@"Documents/array.plist"];

NSLog(@"%@",homeFilePath);

return filePath;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 218.241.181.202 wxhl60 123456 192.168.10.253 wxhl66 wxhl6...
    CYC666阅读 1,561评论 0 6
  • 一、iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭、安全的空间,叫做沙盒。它一...
    tzhtodd阅读 1,364评论 0 2
  • 一、iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭、安全的空间,叫做沙盒。它一...
    1d5cb7cff98d阅读 1,875评论 0 0
  • iOS开发-文件管理(一) 一、iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭...
    MacShare阅读 1,880评论 0 6
  • 大纲 一、文件管理器项目:FileManager0412NSFileManager:文件管理器(单例类)作用:管理...
    AsaGuo阅读 392评论 0 0

友情链接更多精彩内容