文件管理:增。删。移动。复制
//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;
}