前面我在介绍iOS应用存储数据的常用的方式之plist和归档的时候,我们一般存储数据或者写入数据都要建立一个
文件或者文件夹
,所以NSFileManager
在我们日常开发中会经常使用到,这里稍微介绍一下在日常开发过程中,我们会经常对数据进行操作,那么这就会使用到今天我要介绍的
NSFileManager
的类,这个对类是文件和文件夹的操作-
NSFileManager使用了单例模式
- 使用
defaultManager
方法可以获得那个单例对象objc ``[NSFileManager defaultManager]
- 使用
NSFileManager用法
1.文件/文件夹判断
1 - 判断文件或者文件夹是否存在
- (BOOL)fileExistsAtPath:(NSString *)path
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = @"/Users/it001/Desktop/timg.jpeg";
if ([manager fileExistsAtPath:path]) {
NSLog(@"存在");
}else{
NSLog(@"不存在");
}
2 - 判断文件或者文件夹是否存在,
isDirectory
代表是否为文件夹
- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory;
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = @"/Users/it001/Desktop/100";
//
BOOL isdir = NO;
//isDirectory 传入的是一个内存地址,如果这个文件夹真是存在就是yes,如果不存在就是NO
BOOL isSuc = [manager fileExistsAtPath:path isDirectory:&isdir];
NSLog(@"%@---%@",isSuc == YES?@"yes":@"NO",isdir == YES ?@"yes":@"no");
打印结果:yes----yes
3- 下面的方法和上面的是一样 不做例子
-
- (BOOL)isReadableFileAtPath:(NSString *)path;
- path这个文件\文件夹是否可读
-
- (BOOL)isWritableFileAtPath:(NSString *)path;
- path这个文件\文件夹是否可写
- 系统目录不允许写入
-
- (BOOL)isDeletableFileAtPath:(NSString *)path;
- path这个文件\文件夹是否可删除
- 系统目录不允许删除
2.文件的访问
1- 获取文件/文件夹的属性(比如:名称,创建日期,大小,修改时间)
- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error;
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = @"/Users/it001/Desktop/timg.jpeg";
NSDictionary *fileInfo = [manager attributesOfItemAtPath:path error:nil];
NSLog(@"%@",fileInfo);
//打印结果
{
//创建的日期
NSFileCreationDate = "2019-02-12 14:39:02 +0000";
//属性字典对象
NSFileExtendedAttributes = {
"com.apple.lastuseddate#PS" = <8162885c 00000000 64cd143b 00000000>;
"com.apple.metadata:_kMDItemUserTags" = <62706c69 73743030 a0080000 00000000 01010000 00000000 00010000 00000000 00000000 00000000 0009>;
"com.apple.metadata:kMDItemWhereFroms" = 03f8>;
"com.apple.quarantine" = <30303833 3b356336 32646138 623b4368 726f6d65 3b434446 39344535 442d4330 43432d34 3844312d 38463034 2d453234 36374430 38414131 45>;
};
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
//修改时间
NSFileModificationDate = "2019-02-12 14:39:02 +0000";
NSFileOwnerAccountID = 501;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
//文件大小
NSFileSize = 49881;
NSFileSystemFileNumber = 8595530012;
NSFileSystemNumber = 16777220;
NSFileType = NSFileTypeRegular;
2-获取文件内存 NSData数据
- (NSData *)contentsAtPath:(NSString *)path;
+ 获得文件内容
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = @"/Users/lijunchao/Desktop/timg.jpeg";
NSData *contents = [manager contentsAtPath:path];
NSLog(@"%@",contents);
3 - 获取当前path的所有文件(包含子文件夹,但是不会包含子文件夹里面的内容)
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError *)error;
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = @"/Users/lijunchao/Desktop/100";
NSArray *contents = [manager contentsOfDirectoryAtPath:path error:nil];
NSLog(@"%@",contents);
(
abc, //abc子文件夹,没有子文件夹里面的内容
".DS_Store",
"IMG_1436.PNG",
"31.txt"
)
4 - 获取所有文件或者文件夹
- (NSArray *)subpathsAtPath:(NSString *)path;
- (NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError *)error;
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = @"/Users/lijunchao/Desktop/100";
NSArray *contents = [manager subpathsAtPath:path];
NSLog(@"%@",contents);
//打印结果:获取到子文件夹里面的内容
(
abc,
"abc/KVC\U548cKVO\U7684\U4ecb\U7ecd.md",
".DS_Store",
"IMG_1436.PNG",
"31.txt"
)
3.NSFileManager的文件操作
1- 文件的拷贝
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;
NSFileManager *manager = [NSFileManager defaultManager];
//需要注意是路径,在copy的到toPath的时候文件是不存在的,
NSString *path = @"/Users/it001/Desktop/timg.jpeg";
NSString *toPath = @"/Users/it001/Desktop/100/timg.jpeg";
NSError *error = nil;
BOOL isSuc = [manager copyItemAtPath:path toPath:toPath error:&error];
NSLog(@"%@",error.description);
NSLog(@"%@",(isSuc == YES?@"copy成功":@"失败"));
2- 文件的移动(剪切)
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path = @"/Users/it001/Desktop/timg.jpeg";
NSString *toPath = @"/Users/it001/Desktop/100/timg.jpeg";
BOOL isSuc = [manager moveItemAtPath:path toPath:toPath error:nil];
NSLog(@"%@",(isSuc == YES?@"copy成功":@"失败"));
3- 文件的删除
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error;
NSFileManager *manager = [NSFileManager defaultManager];
NSString *toPath = @"/Users/it001/Desktop/100/timg.jpeg";
BOOL isSuc = [manager removeItemAtPath:toPath error:nil];
NSLog(@"%@",(isSuc == YES?@"删除成功":@"失败"));
4- 文件夹的创建
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error;
创建文件夹(createIntermediates为YES代表自动创建中间的文件夹)
NSFileManager *manager = [NSFileManager defaultManager];
NSString *toPath = @"/Users/it001/Desktop/100/CC/timg.jpeg";
BOOL isSuc = [manager createDirectoryAtPath:toPath withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"%@",(isSuc == YES?@"创建成功":@"失败"));
5- 文件的创建
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
创建文件(NSData是用来存储二进制字节数据的)
NSString *str = @"CC";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSFileManager *manager = [NSFileManager defaultManager];
BOOL isSuc = [manager createFileAtPath:@"/Users/it001/Desktop/100/abc.txt" contents:data attributes:nil];
NSLog(@"%@",(isSuc == YES?@"创建成功":@"失败"));