NSFileManger
Foundation
框架的一个类,以单例模式创建的,用于操作磁盘上的文件、文件夹,对他们进行创建、删除、复制、移动…
- 创建对象
//创建单例对象
NSFileManger *fileManger = [NSFileManger defaultManger];
- 常用方法
//判断指定的文件或者文件夹是否在磁盘上存在
-(BOOL)FileExistsAtPath:(NSString *)path;
//判断指定的路径是否有真实存储在我们的磁盘之上,并且判断这个路径是一个文件夹还是一个文件路径
-(BOOL)FileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory;
//返回值代表是否真实存在,参数指针代表这怪路径是否是文件夹路径
//判断指定文件或者文件夹是否可以读取
-(BOOL)isReadableFileAtPath:(NSString *)path;
//判断指定文件或者文件夹是否可以写入
-(BOOL)isWritableFileAtPath:(NSString *)path;
//判断指定文件或者文件夹是否可以删除
-(BOOL)isDeletableFileAtPath:(NSString *)path;
- 常用方法之获取信息
//获取指定文件或者文件夹的属性信息
-(NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error
//返回的是一个字典,拿到特定的信息通过KEY
//获取指定目录下的所有文件和目录
-(NSArray *)subpathsAtPath:(NSString *)path
//获取指定目录下所有子目录和文件 不保护子孙辈
-(NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
- 在指定目录创建文件夹
-(BOOL)createDirctoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error
参数1:路径
参数2:YES,做一路创建,NO不做一路创建
参数3:指定属性 nil为系统默认属性
- 拷贝文件
-(BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;
- 移动文件 剪贴 文件重命名
-(BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;
- 删除文件(直接彻底删除)
-(BOOL)removeItemAtPath:(NSString *)Path error:(NSError **)error;
加载本地plist
根目录的plist
,NSArray
和NSDictionary
都是可以的
NSString *path = [[NSBundle mainBundle]pathForResource:@"car_simple.plist" ofType:nil];
NSArray * arrayDict = [NSArray arrayWithContentsOfFile:path];
数据存储进plist
沙盒路径的plist
,如果存在同名文件将会替换,atomically:YES
是原子属性,写入安全
//沙盒路径
NSString *Document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
//plist文件的路径
NSString *plistPath = [Document stringByApppengdingPathComponent:@"test.plist"]
//存储进plist文件
NSArray *array = @[@"1",@"sb",@"嘻嘻"];
[array writeToFile:Document atomically:YES];
NSURL
//获取本地文件
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"pic.png" withExtension:nil];
获取info.plist文件
NSDictionary *dict = [NSBundle mainBundle].infoDictionary;