/*
写入文件
*/
// NSString * str = @"要写入文件中的内容";
// NSError * error;
// BOOL isWriteSuccess = [str writeToFile:@"/Users/song/Desktop/abc.txt" atomically:YES encoding:NSUTF8StringEncoding error: &error];
// if (isWriteSuccess) {
// NSLog(@"创建成功");
// }else{
// NSLog(@"创建失败:%@",error);
// }
//获取文件管理器
NSFileManager * manager = [NSFileManager defaultManager];
NSString * path = @"/Users/song/Desktop/测试";
NSError * error;
//attributesOfItemAtPath 获取文件属性
NSDictionary * dic = [manager attributesOfItemAtPath:path error:&error];
if (error == nil) {
NSLog(@"%@",dic);
NSLog(@"创建时间:%@",[dic objectForKey:NSFileCreationDate]);
}else{
NSLog(@"error :%@",error);
}
//contentsOfDirectoryAtPath获取文件夹下的内容,只能获取第一级目录
NSArray<NSString *> * array = [manager contentsOfDirectoryAtPath:path error:&error];
NSLog(@"文件夹下一级内容:%@",array);
//subpathsOfDirectoryAtPath获取路径下目录各个层级内容
NSArray * array2 = [manager subpathsOfDirectoryAtPath:path error:&error];
NSLog(@"文件夹下的所有内容%@",array2);
//createDirectoryAtPath创建文件夹
BOOL isCreateSuccess = [manager createDirectoryAtPath:@"/Users/song/Desktop/测试/aaaaa" withIntermediateDirectories:YES attributes:nil error:&error];
if (isCreateSuccess) {
NSLog(@"创建文件成功");
}else{
NSLog(@"创建文件失败");
}
//moveItemAtPath移动目录,相当于剪切操作
BOOL isMoveSuccess = [manager moveItemAtPath:@"/Users/song/Desktop/测试/fedcba" toPath:@"/Users/song/Desktop/测试/abcdef" error:&error];
if (isMoveSuccess) {
NSLog(@"移动成功");
}else{
NSLog(@"移动失败:%@",error);
}
//删除目录
BOOL isRemoveSuccess = [manager removeItemAtPath:@"/Users/song/Desktop/测试/abcdef" error:&error];
if (isRemoveSuccess) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败:%@",error);
}
//拷贝文件
BOOL isCopySuccess = [manager copyItemAtPath:@"/Users/song/Desktop/测试/abc.txt" toPath:@"/Users/song/Desktop/测试/aaaaa/abc.txt" error:&error];
if (isCopySuccess) {
NSLog(@"拷贝成功");
}else{
NSLog(@"拷贝失败:%@",error);
}
//获得文件
NSData * data = [NSData dataWithContentsOfFile:@"/Users/song/Desktop/测试/abc.txt"];
NSLog(@"文件大小:%ld",data.length);
//创建文件
BOOL isCreateFileSuccess = [manager createFileAtPath:@"/Users/song/Desktop/测试/abc2.txt" contents:data attributes:nil];
if (isCreateFileSuccess) {
NSLog(@"创建文件成功");
}else{
NSLog(@"创建文件失败:%@",error);
}
//移动文件
BOOL isMoveFileSuccess = [manager moveItemAtPath:@"/Users/song/Desktop/测试/abc2.txt" toPath:@"/Users/song/Desktop/测试/aaaaa/abccc.txt" error:&error];
if (isMoveFileSuccess) {
NSLog(@"移动文件成功");
}else{
NSLog(@"移动文件失败:%@",error);
}
//删除文件
BOOL isRemoveFileSuccess = [manager removeItemAtPath:@"/Users/song/Desktop/测试/aaaaa/abccc.txt" error:&error];
if (isRemoveFileSuccess) {
NSLog(@"删除文件成功");
}else{
NSLog(@"删除文件失败:%@",error);
}
网友评论