NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [pathArray firstObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"test.txt"];
// 文件属性 -------------------------
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL flag = [fileManager isWritableFileAtPath:filePath];
flag = [fileManager isReadableFileAtPath:filePath];
flag = [fileManager isDeletableFileAtPath:filePath];
flag = [fileManager isExecutableFileAtPath:filePath];
NSArray *componentsArray = [fileManager componentsToDisplayForPath:filePath];
NSLog(@"array = %@", componentsArray);
NSData *data = [fileManager contentsAtPath:filePath];
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str = %@", str);
NSLog(@"flag = %i", flag);
//创建目录、创建文件、拷贝文件、删除文件 -------------------------
NSString *dir = [documentPath stringByAppendingPathComponent:@"a/b"];
BOOL exists = [fileManager fileExistsAtPath:dir];
if(!exists)
{
[fileManager createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil];
}
// 创建文件
NSString *ffilePath = [dir stringByAppendingPathComponent:@"test.txt"];
if(![fileManager fileExistsAtPath:ffilePath isDirectory:NO])
{
[fileManager createFileAtPath:ffilePath contents:data attributes:nil];
}
NSLog(@"ffilepath = %@", ffilePath);
// 拷贝文件
NSString *dictFilePath = [[ffilePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"test_copy.txt"];
[fileManager copyItemAtPath:ffilePath toPath:dictFilePath error:nil];
// 删除文件
[fileManager removeItemAtPath:ffilePath error:nil];
// 获取指定目录下的文件和文件夹
// 1.contentsOfDirectoryAtPath 只遍历当前目录下的,子目录下的文件不遍历
NSArray *dirArray = [fileManager contentsOfDirectoryAtPath:documentPath error:nil];
NSLog(@"dirArray = %@", dirArray);
// 2 enumeratorAtPath 获取目录下的所有文件,包括所有子目录
NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:documentPath];
NSMutableArray *dirArray2 = [[NSMutableArray alloc]initWithCapacity:1];
while ([dirEnum nextObject])
{
[dirArray2 addObject:[dirEnum nextObject]];
}
NSLog(@"dirArray2 = %@", dirArray2);