NSFileManager

/* 两种获取文件路径的方式 */ //绝对路径 NSString*directPath=@"/Users/cuixi/desktop"; //目录路径,直接通过路径获取 //通过函数获取路径 NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES); NSString*deskPath=paths[0]; //文件路径 NSString*filePatstrong=[directPath stringByAppendingPathComponent:@"myfile.txt"]; NSLog(@"direc==%@",directPath); NSString*filePatstrong=[deskPath stringByAppendingPathComponent:@"myfile.txt"]; NSLog(@"desk==%@",deskPath); NSFileManager*manager=[NSFileManager defaultManager]; NSLog(@"f1=%@,f2=%@",filePatstrong,filePatstrong); if ([manager fileExistsAtPath:filePatstrong]) { NSLog(@"myfile.txt exist in filePatstrong"); } if([manager fileExistsAtPath:filePatstrong]){ NSLog(@"myfile.txt exist in filePatstrong"); } //赋值、移动、删除 // 仅仅是移动 if ([manager copyItemAtPath:@"/Users/cuixi/desktop/myfile.txt" toPath:@"/Users/cuixi/desktop/python学习/myfile.txt" error:nil]) { NSLog(@"移动文件success"); } // 移动和改名,原来位置的文件不在了,在新的位置。 if ([manager moveItemAtPath:@"/Users/cuixi/desktop/myfile.txt" toPath:@"/Users/cuixi/desktop/python学习/moveItem.txt" error:nil]) { NSLog(@"移动文件,修改文件名称success"); } //删除文件 if ([manager removeItemAtPath:@"/Users/cuixi/desktop/python学习/moveItem.txt" error:nil]&&[manager removeItemAtPath:@"/Users/cuixi/desktop/python学习/myfile.txt" error:nil]) { NSLog(@"删除文件success"); }