//在.m文件里
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
#pragma mark --NSFileManager 文件管理器
#pragma mark 1.创建文件夹
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [array lastObject];
NSString *filePath = [path stringByAppendingPathComponent:@"zhaoYing"];
//1.创建文件管理器对象
NSFileManager *fileManager = [NSFileManager defaultManager];
#warning 重点 2、创建文件夹
[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"filePath = %@", filePath);
NSString *Str = @"大好人";
NSString *filePathZhaoY = [filePath stringByAppendingPathComponent:@"zhaoYing.txt"];
#warning 重点 写入本地 writeToFile
[Str writeToFile:filePathZhaoY atomically:YES encoding:NSUTF8StringEncoding error:nil];
//再创建一个文件夹
NSString *filePathJCZ = [path stringByAppendingPathComponent:@"JiChangZhi"];
[fileManager createDirectoryAtPath:filePathJCZ withIntermediateDirectories:YES attributes:nil error:nil];
//将文件夹下的zhaoying.txt移动到JiChangZhi文件夹里
NSString *filePathJCZT = [filePathJCZ stringByAppendingPathComponent:@"zhaoYing1.txt"];
/*
BOOL result = [fileManager moveItemAtPath:filePathZhaoY toPath:filePathJCZT error:nil];
if (result) {
NSLog(@"移动成功");
}else
{
NSLog(@"移动失败");
}
*/
//拷贝文件到另一个目录中
BOOL copyResult = [fileManager copyItemAtPath:filePathZhaoY toPath:filePathJCZT error:nil];
if (copyResult) {
NSLog(@"复制成功");
}else
{
NSLog(@"复制失败");
}
#warning 重点 删除文件
BOOL removeResult = [fileManager removeItemAtPath:filePathZhaoY error:nil];
if (removeResult) {
NSLog(@"删除成功");
}else
{
NSLog(@"删除失败");
}
#warning 重点 判断文件是否存在
BOOL existsReuslt = [fileManager fileExistsAtPath:filePathJCZT];
if (existsReuslt) {
NSLog(@"文件存在");
}else
{
NSLog(@"文件不存在");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end