NSFileManager的應用,NSFileManager應用
單例,是在 一個檔案中只建立一次就能夠全部一起共用,多建立的地址是相同的NSFileManager *manager=[NSFileManager defaultManager];是一個單例 NSFileManager *manager=[NSFileManager defaultManager];
//判斷一個檔案或檔案夾是否存在
//參數a表示參看檔案是不是目錄,如果是返回yes,如果是內容,則返回no
//參數b表示檔案夾是否存在
BOOL a;
BOOL b= [manager fileExistsAtPath:@"/Users/zhangyunjiang/Desktop/視頻1/惡棍天使流暢.mp4" isDirectory:&a]; NSLog(@"%d %d",b,a); //擷取檔案或檔案夾的屬性
NSDictionary *dic= [manager attributesOfItemAtPath:@"/Users/zhangyunjiang/Desktop/視頻1/惡棍天使流暢.mp4" error:nil]; NSLog(@"%@",dic); //擷取檔案中所有的檔案(弊端不能擷取檔案的檔案中裡面的資料)
NSArray *arr=[manager contentsOfDirectoryAtPath:@"/Users/zhangyunjiang/Desktop/視頻1" error:nil];
NSLog(@"%@",arr);
//用subpathsAtPath可以擷取檔案的檔案裡面的資料
NSArray *arr1=[manager subpathsAtPath:@"/Users/zhangyunjiang/Desktop/視頻1"]; NSLog(@"%@",arr1); //拷貝 BOOL a=[manager copyItemAtPath:@"/Users/zhangyunjiang/Desktop/c語言/app c 練習/app" toPath:@"/Users/zhangyunjiang/Desktop/未命名檔案夾" error:nil]; NSLog(@"%d",a);//刪除 BOOL b=[manager removeItemAtPath:@"/Users/zhangyunjiang/Desktop/zyj" error:nil]; NSLog(@"%d",b);//移動(剪下) BOOL c= [manager moveItemAtPath:@"/Users/zhangyunjiang/Desktop/未命名檔案夾/app" toPath:@"/Users/zhangyunjiang/Desktop/zyj" error:nil]; NSLog(@"%d",c); //建立檔案夾 BOOL a= [manager createDirectoryAtPath:@"/Users/zhangyunjiang/Desktop/zyj/jiuh/sdgre/fsdg" withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"%d",a);
//建立檔案 BOOL b=[manager createFileAtPath:@"/Users/zhangyunjiang/Desktop/frty.txt" contents:nil attributes:nil]; NSLog(@"%d",b);