标签:exist content 获取文件属性 mat 创建文件 create min ack 获取文件
//NSFileManager
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",NSHomeDirectory());
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [docPath stringByAppendingPathComponent:@"firstFM"];
NSFileManager *fm = [NSFileManager defaultManager];
//一.创建
//1.创建文件夹
if (![fm fileExistsAtPath:filePath]) {
NSError *error = nil;
BOOL isSuc = [fm createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
if (!isSuc) {
NSLog(@"fail:%@",error.localizedDescription);
}
}else{
NSLog(@"file existed");
}
//2.创建文件
NSString *txtPath = [filePath stringByAppendingPathComponent:@"firstTxt.txt"];
NSString *test = @"hello world";
NSData *writeData= [test dataUsingEncoding:NSUTF8StringEncoding];
if (![fm fileExistsAtPath:txtPath]) {
BOOL isSuc = [fm createFileAtPath:txtPath contents:writeData attributes:nil];
if (!isSuc) {
NSLog(@"fail");
}
}else{
NSLog(@"txt existed");
}
//1.
// [self scanDirectoryOrFile:filePath];
//2.
NSString *sourcePath = txtPath;
NSString *newPath = [docPath stringByAppendingPathComponent:@"firstTxt.txt"];
// [self moveDiectoryOrFile:sourcePath toNewPath:newPath];
// [self deleteDirectoryOrFile:[docPath stringByAppendingPathComponent:@"rrr"]];
[self attributeForDirectoryOrFile:newPath];
}
//二.操作
//1.遍历文件夹(目录)
- (void)scanDirectoryOrFile:(NSString *)path{
NSFileManager *fm = [NSFileManager defaultManager];
//浅层遍历(只包含当前路径的子目录)
if ([fm fileExistsAtPath:path]) {
//数组包含的是文件夹的名字
NSArray *arr1 = [fm contentsOfDirectoryAtPath:path error:nil];
NSLog(@"%ld--%@",arr1.count,arr1.firstObject);
}
//深层遍历(包含路径下所有子文件)
// if ([fm fileExistsAtPath:path]) {
// NSArray *arrAll = [fm subpathsOfDirectoryAtPath:path error:nil];
// }
}
//2.拷贝
- (void)copyDirectoryOrFile:(NSString *)sourcePath toNewPath:(NSString *)newPath{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
BOOL isSuc = [fm copyItemAtPath:sourcePath toPath:newPath error:&error];
if (isSuc) {
NSLog(@"success");
}else{
NSLog(@"%@",error.localizedDescription);
}
}
//3.移动(剪切)
- (void)moveDiectoryOrFile:(NSString *)sourcePath toNewPath:(NSString *)newPath{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
BOOL isSuc = [fm moveItemAtPath:sourcePath toPath:newPath error:&error];
if (isSuc) {
NSLog(@"success");
}else{
NSLog(@"%@",error.localizedDescription);
}
}
//4.删除
- (void)deleteDirectoryOrFile:(NSString *)path{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
BOOL isSuc = [fm removeItemAtPath:path error:nil];
if (isSuc) {
NSLog(@"success");
}else{
NSLog(@"%@",error.localizedDescription);
}
}
//三.获取文件属性
- (void)attributeForDirectoryOrFile:(NSString *)path{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
NSDictionary *dic = [fm attributesOfItemAtPath:path error:&error];
NSLog(@"%@",dic);
}
标签:exist content 获取文件属性 mat 创建文件 create min ack 获取文件
原文地址:http://www.cnblogs.com/daxueshan/p/6954106.html