1.NSFileManager:单例,对文件进行操作
[NSFileManager defaultManager]
2.UIImage转NSData
NSData *imageData = UIImagePNGRepresentation(image);
3.NSHomeDirectory:获取应用的主路径
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/pic"];
4.fileExistsAtPath判断路径下是否存在这个文件
BOOL isExit = [[NSFileManager defaultManager] fileExistsAtPath:path];
5.createFileAtPath:在路径上创建文件
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
6.createDirectoryAtPath:在路径上创建目录
[[NSFileManager defaultManager] createDirectoryAtPath:dir2Path
withIntermediateDirectories:YES attributes:nil error:nil];
7.将文件转化为NSData
NSData *data = [NSData dataWithContentsOfFile:path];
8.获得应用的Document目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) lastObject];
9.moveItemAtPath:toPath:将第一个路径上的文件移动到另一个路径
[[NSFileManager defaultManager] moveItemAtPath:path toPath:[dir1Path stringByAppendingPathComponent:@"pic1"]
error:&errorMsg];
10.copyItemAtPath:toPath:将第一个路径上的文件拷贝粘贴到另一个路径上
[[NSFileManager defaultManager] copyItemAtPath:[dir1Path stringByAppendingPathComponent:@"pic1"]
toPath:[dir2Path stringByAppendingPathComponent:@"pic2"] error:&errorMsg];
11.removeItemAtPath:删除文件
[[NSFileManager defaultManager] removeItemAtPath:dir1Path error:nil];
小例子:
@interface ViewController ()
@property (nonatomic, strong) NSFileManager *fileManager;
@end
///Users/a/Library/Developer/CoreSimulator/Devices/4DF9F8F0-E0BD-4EFC-AC49-340DAE285D62/data/Containers/Data/Application/77EA378C-8B16-424E-8607-456EAC48EC7A/Documents/pic
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取系统提供文件管理器 单例
self.fileManager = [NSFileManager defaultManager];
//读取图片
UIImage *image = [UIImage imageNamed:@"4.jpg"];
//UIImage -> NSData//二进制
NSData *imageData = UIImagePNGRepresentation(image);
// NSLog(@"%@", imageData);
//创建文件
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/pic"];//添加的是路径的一部分
NSLog(@"%@", path);
//创建文件
if (![self.fileManager fileExistsAtPath:path]) {
//文件不存在
//创建一个空的文件
[self.fileManager createFileAtPath:path contents:nil attributes:nil];
//写入内容
[imageData writeToFile:path atomically:YES];
}
//读取文件
if ([self.fileManager fileExistsAtPath:path]) {
NSData *data = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:data];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
//创建两个目录:目录1 目录2
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];//自动寻找可操作的目录,但是只有一个
// NSLog(@"%@", dir1Path);
//两个方法不同
NSString *dir1Path = [docPath stringByAppendingString:@"/目录1"];
NSString *dir2Path = [docPath stringByAppendingPathComponent:@"目录2"];
// NSLog(@"%@, %@", dir1Path, dir2Path);
// BOOL *isExist;
if (![self.fileManager fileExistsAtPath:dir1Path]) {
//不存在,创建
[self.fileManager createDirectoryAtPath:dir1Path withIntermediateDirectories:YES attributes:nil error:nil];
}
if (![self.fileManager fileExistsAtPath:dir2Path]) {
//不存在,创建
[self.fileManager createDirectoryAtPath:dir2Path withIntermediateDirectories:YES attributes:nil error:nil];
}
//将Documents/pic 移动到Documents/目录1/pic1,移动的时候还要设置文件名
NSError *errorMsg = NULL;
[self.fileManager moveItemAtPath:path toPath:[dir1Path stringByAppendingPathComponent:@"pic1"] error:&errorMsg];
if (errorMsg) {
//有错误信息
NSLog(@"errorMsg:%@", errorMsg);
}else{
NSLog(@"move ok");
}
//将目录1/pic1 拷贝copy 到目录2/pic1
[self.fileManager copyItemAtPath:[dir1Path stringByAppendingPathComponent:@"pic1"] toPath:[dir2Path stringByAppendingPathComponent:@"pic2"] error:&errorMsg];
if (errorMsg) {
//有错误信息
NSLog(@"errorMsg:%@", errorMsg);
}else{
NSLog(@"copy ok");
}
//删除文件
[self.fileManager removeItemAtPath:dir1Path error:nil];
}
@end