【iOS系列】-文件管理
OC中操作文件,需要使用NSFileManager:
需要使用NSFileManager的创建方式:
//单例模式创建对象
NSFileManager * f2 = [NSFileManager defaultManager];
//普通方式创建对象
NSFileManager * f3 = [[NSFileManager alloc] init];
1.获取文件管理器
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/test.txt";
NSError * error;
NSDictionary * dic = [fileManager attributesOfItemAtPath:path error:&error];
NSLog(@"dic %@",dic);
if (error == nil)
{
NSDate * date = [dic objectForKey:NSFileCreationDate];//文件创建时间
NSString * size = [dic objectForKey:NSFileSize];//文件大小
NSLog(@" date = %@, size = %@",date,size);
}
2.获取目录下的文件与子目录列表
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/other";
NSError * error;
//能够逐级的获得所有子集的目录
NSArray * array = [fileManager subpathsOfDirectoryAtPath:path error:&error];
//只能获得目标目录下,第一级目录
NSArray * array = [fileManager contentsOfDirectoryAtPath:path error:&error];
NSLog(@"array %@",array);
目录管理:
1.创建目录
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/myfolder/aaa";
NSError * error;
//withIntermediateDirectories;YES 逐级创建文件夹,NO表示只能够创建一级目录
BOOL isCreateSuccess = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
if (isCreateSuccess)
{
NSLog(@"创建成功");
}
else
{
NSLog(@"error %@",error);
}
2.移动目录
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/myfolder";
NSString * pathTo = @"/Users/aplle/Desktop/newmyfolder";
NSError * error ;
BOOL isMoveSuccess = [fileManager moveItemAtPath:path toPath:pathTo error:&error];
if (isMoveSuccess)
{
NSLog(@"移动成功");
}
else
{
NSLog(@"error %@",error);
}
3.删除目录:
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/newmyfolder";
NSError * error;
BOOL isRemoveSuccess = [fileManager removeItemAtPath:path error:&error];
if (isRemoveSuccess)
{
NSLog(@"删除成功");
}
else
{
NSLog(@"erro %@",error);
}
4.拷贝目录:
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/myfolder";
NSString * pathTo = @"/Users/aplle/Desktop/newmyfolder";
NSError * error;
BOOL isCopySuccess = [fileManager copyItemAtPath:path toPath:pathTo error:&error];
if(isCopySuccess)
{
NSLog(@"拷贝成功");
}
else
{
NSLog(@"error %@",error);
}
文件管理
1.复制文件
//mp3.mp4 rmvb mkv pdf doc txt,图片等都可用NSData读取
NSString * path = @"/Users/aplle/Desktop/屏幕快照 .png";
NSData * data = [NSData dataWithContentsOfFile:path];
NSLog(@"%ld",data.length);
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * pathTo = @"/Users/aplle/Desktop/屏幕快照副本.png";
BOOL isCreateSuccess = [fileManager createFileAtPath:pathTo contents:data attributes:nil];
if (isCreateSuccess)
{
NSLog(@"创建成功");
}
else
{
NSLog(@"创建失败");
}
2.移动文件
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/屏幕快照 .png";
NSString * pathTo = @"/Users/aplle/Desktop/newmyfolder/屏幕屏幕.png";
NSError * error;
BOOL ismoveSuccess = [fileManager moveItemAtPath:path toPath:pathTo error:&error];
if (ismoveSuccess)
{
NSLog(@"移动成功");
}
else
{
NSLog(@"error %@",error);
}
3.删除文件
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/屏幕快照副本.png";
NSError * error;
BOOL isremove = [fileManager removeItemAtPath:path error:&error];
if (isremove)
{
NSLog(@"删除成功");
}
else
{
NSLog(@"error %@",error);
}
//NSMutableData
//当需要把多个NSData数据拼接成一个数据存储的时候,就要想到使用NSMutableData这个类型
NSString * str1 = @"好好学习";
NSString * str2 = @"天天向上";
NSString * str3 = @"今天休息";
NSMutableData * muData = [[NSMutableData alloc] init];
NSData * d1 = [str1 dataUsingEncoding:NSUTF8StringEncoding];
NSData * d2 = [str2 dataUsingEncoding:NSUTF8StringEncoding];
NSData * d3 = [str3 dataUsingEncoding:NSUTF8StringEncoding];
//appendData 能够把nsdata对象加入到 muData对象中
[muData appendData:d1];
[muData appendData:d2];
[muData appendData:d3];
NSString * path = @"/Users/aplle/Desktop/123.txt";
//NSMutableData是继承至NSData的所以可以调用writeToFile 把数据写入到一个指定的目录下
BOOL iswriteSucess = [muData writeToFile:path atomically:YES];
if (iswriteSucess)
{
NSLog(@"创建成功");
}
else
{
NSLog(@"创建失败");
}