iOS NSFileManager 使用详解

使用NSFileManager

文件系统接口

允许访问文件夹内容

创建 重命名 删除文件 修改文件和文件属性,以及Finder对所有文件系统任务执行的一般操作。

访问NSFileManager,使用共享的管理器对象

NSFileManager *fileManager = [NSFileManager defaultManager];

允许对NSFileManager设置代理

用于当文件管理器完成如 复制或移动文件操作时 接收相应的信息。

需要创建自己的NSFileManager示例,而不是使用共享实例。

NSFileManager *newFileManager = [[NSFileManager alloc] init];
newFileManager.delegate = self;

获取一个文件夹的内容

使用方法

contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:

简单返回文件夹内容的NSURL

NSURL *folderURL = [NSURL fileURLWithPath:@"/Applications"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError __autoreleasing *error = nil;
NSArray *folderContents = [fileManager contentsOfDirectoryAtURL:folderURL includingPropertiesForKeys:nil options:0 error:&error];
NSLog(@"返回文件夹内容的URL:%@",folderContents);

folderContents包含指向该文件夹中每一项的NSURL。


访问单独的NSURL对象,获取指向的文件信息

使用方法: resourceValuesForKeys:error:

返回NSDictionary,包含每一项指向的文件的属性 可以使用此方法获取缓存大小

 //创建一个数组 包含想要了解的属性
    //这里包含文件大小 修改日期
    NSArray *attributes = [NSArray arrayWithObjects:NSURLFileSizeKey,NSURLContentModificationDateKey,nil];
    //获得返回的结果
    //[NSURL URLWithString:@""] 是一个NSURL对象 想要了解的文件夹
    NSDictionary *attributesDictionary = [[NSURL URLWithString:@""] resourceValuesForKeys:attributes error:nil];
    //获取文件的大小
    NSNumber *fileSizeInBytes = [attributesDictionary objectForKey:NSURLFileSizeKey];
    //获取最近修改的日期
    NSDate *lastModifileDate = [attributesDictionary objectForKey:NSURLContentModificationDateKey];

还可以在NSFileManager列出文件夹内容时,预抓取属性

NSArray *attributes = @[NSURLFileSizeKey,NSURLContentModificationDateKey];
    NSURL *folderURL = [NSURL fileURLWithPath:path];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError __autoreleasing *error = nil;
    NSArray *folderContents = [fileManager contentsOfDirectoryAtURL:folderURL includingPropertiesForKeys:attributes options:0 error:&error];
    NSLog(@"返回文件夹内容的URL:%@",folderContents);

创建目录

使用方法:[fileManager createDirectoryAtURL:anURL withIntermediatetDirectories:YES attributes:nil error:nil]

withIntermediatetDirectories:YES 表示创建额外 需要的文件夹 创建父目录不存在的子目录,自动将父目录创建

创建文件:

使用方法

[fileManager createFileAtPath:aPath contents:someData attributes:nil];

删除文件

使用方法

[fileManager removeItemAtURL:anURL error:nil];

这样删除不回移至垃圾箱

移动文件

使用方法

[file moveAtURL:sourceURL toURL:destinationURL error:nil]; -> BOOL

复制文件

使用方法

[file copyItemAtURL:sourceURL toURL:destinationURL error:nil]; -> BOOL

浅度遍历目录

使用方法

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error

深度遍历目录

使用方法

- (NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error

获取当前的目录

- (NSString *)currentDirectoryPath

更改当前目录

- (BOOL)changeCurrentDirectoryPath:(NSString *)path

枚举目录内容

- (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path

判断文件是否存在

- (BOOL)fileExistsAtPath:(NSString *)path

获取文件信息(属性和权限)

- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error

从文件中读取数据

- (NSData *)contentsAtPath:(NSString *)path

比较两个文件的内容

- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2

测试文件是否存在 能否执行读操作

- (BOOL)isReadableFileAtPath:(NSString *)path

测试文件是否存在 能否执行写操作

- (BOOL)isWritableFileAtPath:(NSString *)path

二、文件操作类NSFileHandle常用操作:

只读方式打开文件

+ (id)fileHandleForReadingAtPath:(NSString *)path

只写方式打开文件

+ (id)fileHandleForWritingAtPath:(NSString *)path

读写方式打开文件

+ (id)fileHandleForUpdatingAtPath:(NSString *)path

从文件当前位置读到结尾

- (NSData *)readDataToEndOfFile

从文件当前位置读到固定字节数的内容

- (NSData *)readDataOfLength:(NSUInteger)length

返回所有可用的数据

- (NSData *)availableData

写文件

- (void)writeData:(NSData *)data

固定到文件尾部

- (unsigned long long)seekToEndOfFile

定位到文件指定位置

- (void)seekToFileOffset:(unsigned long long)offset

获取当前文件的偏移量

- (unsigned long long)offsetInFile

将文件的长度设置为offset字节

- (void)truncateFileAtOffset:(unsigned long long)offset

 关闭文件

- (void)closeFile
P.S. (网络socket中)通过initWithFileDescriptor初始化的对象,需要显式调用此方法;其它方法创建的对象会自动打开文件,该对象被销毁时会自动关闭该方法,不需显式调用此方法。

参考博客 http://www.cnblogs.com/jy578154186/archive/2013/02/27/2935152.html

原文地址:https://www.cnblogs.com/huanying2000/p/6222713.html