Objective-C NSFileManager的使用
2015-08-18 18:13
579 查看
1. 回顾
转大写
-(NSString*)uppercaseString;
-(NSString*)lowercaseString;
-(NSString*)capitalizedString;
-(BOOL)isEqualToString:(NSString*)aString;
-(NSComparisonResult)compare:(NSString*)aString;
-(BOOL)hasPrefix:(NSString*)aString;
-(BOOL)hasSuffix:(NSString*)aString;
-(NSRange)rangeOfString:(NSString*)aString;
反向搜索
-(NSRange)rangeOfString:(NSString*)aString options:NSBackwardsSearch;
-(NSString*)substringFromIndex:(NSUInteger)from;
-(NSString*)substringToIndex:(NSUInteger)to;
-(NSString*)substringWithRange:(NSRange)range;
-(unichar)characterAtIndex:(NSUInteger)index;
字符串转换基础数据类型
-(double)doubleValue;
-(int)intValue;
-(float)floatValue;
-(char*)UTF8String;//一般在sqlite数据库时才会用到
+(instancetype)array;
+(instancetype)arrayWithObject:(id)anObject;
+(instancetype)arrayWithObjects:(id)firstObje,...;
+(instancetype)arrayWithArray:(NSArray*)array;
+(id)arrayWithContentsOfFile:(NSString*)filepath;
NSArray不能存储nil
-(id)objectAtIndex:(NSUInteger)index;
for(id obj in array){ }
[arry enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){ }];
-(BOOL)writeToFile:(NSString*)path atomically:(BOOL) useAuxiliaryFile;
-(BOOL)writeToURL:(NSURL*)url atomically:(BOOL)atomically;
字符串拼接
-(NSString*)componentsJoinedBytring:(NSString*)separator;
-(NSArray*)componentsSeparatedByString:(NSString*)separator;
+(instancetype)array;
-(void)addObject:(id)object;
-(void)insertObject:(id)anObject atIndex:(NSUInteger)index;
-(void)removeObjectAtIndex:(NSUInteger) index;
-(void)removeObjectsInRange:(NSRange)range;
-(void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
+(instancetype)dictionary;
+(instancetype)dictionaryWithObject:(id)object forKey:(id<>)
快速创建
NSArray *array = @[@"",@"",@"",@""];
NSDictioanry *dic = @{@"":@"",@"":@"",@"":@""};
for(NSString *str in dict) { }
-(id)objectForKey:(NSUInteger)index;
[dic dictionaryEnumerateObjectsUsingBlock:^(id key ,id value , BOOL *stop){}];
[dic writeToFile:@"/Users/apple user/Desktop/dic.plist" atomically:YES];
-(void)setObject:(id)anObject forKey:(id<NSCoping>)akey;
-(void)removeObject:(NSString*)akey;
2.NSFileManager的基本用法
NSFileManager是用来管理文件系统的
它可以用来进行常见的文件及文件夹操作(拷贝 ,剪切,创建等)
NSFileManager使用单例模式
使用defaultManager 方法尅获得那个单例模式
-(BOOL)fileExistsAtPath:(NSString*)path;
-(void)fileExistsAtPath:(NSString*)path isDirectory:(BOOL *)isDir;
NSString *filePath = @"/Users/apple/Desktop/1.txt";
NSString *directoryPath = @"/Users/apple/Desktop/";
//fm该对象是一个单例对象
NSFileManager *fm = [NSFileManager defaultManager];
//1.文件是否存在
BOOL isYES = [fm fileExistsAtPath:filePath];
//2.判断是否是一个目录,还是一个文件夹
BOOL isDir;
[fm fileExistsAtPath:filePath isDirectory:&isDir];
if(isDir)
NSLog(@"这是一个目录");
//3.判断文件是否可读
BOOL isRead;
isRead = [fm isReadableFileAtPath:filepath];
if(isRead)
NSLog(@"文件可读");
//4.是否可写
BOOL isWrite;
isWrite = [fm isWritableFileAtPath:filePath];
if(isWrite)
NSLog(@"文件可写");
//5.是否可删除
BOOL isDelete;
isDelete = [fm isDeletableFileAtPath:filePath];
if(isDelete)
NSLog(@"文件可删除");
NSFileManager 文件访问
NSFileManager *fm = [NSFileManager defaultManager];
NSString *filepath = @"/Users/apple/Desktop/a.plist";
//1.获取文件属性信息
NSDictionary *dicInfo = [fm attributesOfItemAtPath:filepath error:nil];
NSLog(@"%@",dicInfo);
NSLog(@"%@",dicInfo["NSFileOwnerAccountName"]);
//2.获取指定目录下的文件及子文件
//以递归的方式,获取当前目录及子目录下的所有文件及文件夹
filepath = @"/Users/apple/Desktop/test/";
NSArray *arrSubPath = [fm subpathsAtPath:filepath];
NSLog(@"%@",arrSubPath);
//不是以递归的方式获取
NSArray *arrDir = [fm subpathsOfDirectoryAtPath:filepath error:nil];
NSLog(@"%@",arrDir);
//3.获取指定目录下的文件及目录信息
arrDir = [fm contentsOfDirectoryAtPath:filepath error:nil];
NSLog(@"%@",arrDir);
NSFileManger 创建目录及用法
NSFileManager *fm = [NSFileManager defaultManager];
//1.创建目录
NSString *createPath = @"/Users/apple/Desktop/test1";
NSDictionary *dicInfo = [NSDictionary dictionary];
BOOL isYES = [fm createDirectoryAtPath:createPath withIntermediateDirectories:NO attributes:dicInfo error:nil];
//注意:withIntermediateDirectories这个属性是路径中间的目录如果没有是否跟着创建
if(isYES)
NSLog(@"目录创建成功!");
//2.创建文件
NSString *filepath = @"/Users/apple/Desktop/test/a.plist";
NSDictionary *dicFile = [NSDictionary dictionary];
//NSData是一个二进制数据的类,比如下载文件比较大,就需要一部分一部分下载
NSString *strContent = @"这是我要存储的内容";
NSData *data = [strContent dataUsingEncoding:NSUTF8String ];
BOOL isYES = [fm createFileAtPath:filepath contents:data attributes:dicFile ];
if(isYES)
NSLog(@"文件创建及写入成功!");
//3.copy文件
NSString *path1 = @"/Users/apple/Desktop/test/a1.plist";
NSString *path2 = @"/Users/apple/Desktop/";
BOOL isYes = [fm copyItemAtPath:path1 toPath:path2 error:nil];
if(isYES)
NSLog(@"拷贝文件成功!");
//4.移动文件
NSString *path1 = @"/Users/apple/Desktop/test/a1.plist";
NSString *path2 = @"/Users/apple/Desktop/";
BOOL isYES = [fm moveItemAtPath:path1 toPath:path2 error:nil];
if(isYES)
NSLog(@"移动文件成功");
//5.删除文件
NSString *dpath = @"/Users/apple/Desktop/test/a1.plist";
BOOL isYES = [fm deleteItemAtPath:dpath error:nil];
if(isYES)
NSLog(@"文件删除成功!");
3.NSFileManager文件下载思路
例如:更新软件,过程就是远程下载文件,然后更新文件
步骤:
1.要下载的文件在服务器
2.客服端连接服务器,通过一个地址,使用Http协议
3.返回文件大小及信息到客户端,以二进制流的形式
4.服务器把文件分解多个小文件
要求:会断点续传,就是今天下载了一点,明天在来下载,那么明天还可以继续接着下载
转大写
-(NSString*)uppercaseString;
-(NSString*)lowercaseString;
-(NSString*)capitalizedString;
-(BOOL)isEqualToString:(NSString*)aString;
-(NSComparisonResult)compare:(NSString*)aString;
-(BOOL)hasPrefix:(NSString*)aString;
-(BOOL)hasSuffix:(NSString*)aString;
-(NSRange)rangeOfString:(NSString*)aString;
反向搜索
-(NSRange)rangeOfString:(NSString*)aString options:NSBackwardsSearch;
-(NSString*)substringFromIndex:(NSUInteger)from;
-(NSString*)substringToIndex:(NSUInteger)to;
-(NSString*)substringWithRange:(NSRange)range;
-(unichar)characterAtIndex:(NSUInteger)index;
字符串转换基础数据类型
-(double)doubleValue;
-(int)intValue;
-(float)floatValue;
-(char*)UTF8String;//一般在sqlite数据库时才会用到
+(instancetype)array;
+(instancetype)arrayWithObject:(id)anObject;
+(instancetype)arrayWithObjects:(id)firstObje,...;
+(instancetype)arrayWithArray:(NSArray*)array;
+(id)arrayWithContentsOfFile:(NSString*)filepath;
NSArray不能存储nil
-(id)objectAtIndex:(NSUInteger)index;
for(id obj in array){ }
[arry enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){ }];
-(BOOL)writeToFile:(NSString*)path atomically:(BOOL) useAuxiliaryFile;
-(BOOL)writeToURL:(NSURL*)url atomically:(BOOL)atomically;
字符串拼接
-(NSString*)componentsJoinedBytring:(NSString*)separator;
-(NSArray*)componentsSeparatedByString:(NSString*)separator;
+(instancetype)array;
-(void)addObject:(id)object;
-(void)insertObject:(id)anObject atIndex:(NSUInteger)index;
-(void)removeObjectAtIndex:(NSUInteger) index;
-(void)removeObjectsInRange:(NSRange)range;
-(void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
+(instancetype)dictionary;
+(instancetype)dictionaryWithObject:(id)object forKey:(id<>)
快速创建
NSArray *array = @[@"",@"",@"",@""];
NSDictioanry *dic = @{@"":@"",@"":@"",@"":@""};
for(NSString *str in dict) { }
-(id)objectForKey:(NSUInteger)index;
[dic dictionaryEnumerateObjectsUsingBlock:^(id key ,id value , BOOL *stop){}];
[dic writeToFile:@"/Users/apple user/Desktop/dic.plist" atomically:YES];
-(void)setObject:(id)anObject forKey:(id<NSCoping>)akey;
-(void)removeObject:(NSString*)akey;
2.NSFileManager的基本用法
NSFileManager是用来管理文件系统的
它可以用来进行常见的文件及文件夹操作(拷贝 ,剪切,创建等)
NSFileManager使用单例模式
使用defaultManager 方法尅获得那个单例模式
-(BOOL)fileExistsAtPath:(NSString*)path;
-(void)fileExistsAtPath:(NSString*)path isDirectory:(BOOL *)isDir;
NSString *filePath = @"/Users/apple/Desktop/1.txt";
NSString *directoryPath = @"/Users/apple/Desktop/";
//fm该对象是一个单例对象
NSFileManager *fm = [NSFileManager defaultManager];
//1.文件是否存在
BOOL isYES = [fm fileExistsAtPath:filePath];
//2.判断是否是一个目录,还是一个文件夹
BOOL isDir;
[fm fileExistsAtPath:filePath isDirectory:&isDir];
if(isDir)
NSLog(@"这是一个目录");
//3.判断文件是否可读
BOOL isRead;
isRead = [fm isReadableFileAtPath:filepath];
if(isRead)
NSLog(@"文件可读");
//4.是否可写
BOOL isWrite;
isWrite = [fm isWritableFileAtPath:filePath];
if(isWrite)
NSLog(@"文件可写");
//5.是否可删除
BOOL isDelete;
isDelete = [fm isDeletableFileAtPath:filePath];
if(isDelete)
NSLog(@"文件可删除");
NSFileManager 文件访问
NSFileManager *fm = [NSFileManager defaultManager];
NSString *filepath = @"/Users/apple/Desktop/a.plist";
//1.获取文件属性信息
NSDictionary *dicInfo = [fm attributesOfItemAtPath:filepath error:nil];
NSLog(@"%@",dicInfo);
NSLog(@"%@",dicInfo["NSFileOwnerAccountName"]);
//2.获取指定目录下的文件及子文件
//以递归的方式,获取当前目录及子目录下的所有文件及文件夹
filepath = @"/Users/apple/Desktop/test/";
NSArray *arrSubPath = [fm subpathsAtPath:filepath];
NSLog(@"%@",arrSubPath);
//不是以递归的方式获取
NSArray *arrDir = [fm subpathsOfDirectoryAtPath:filepath error:nil];
NSLog(@"%@",arrDir);
//3.获取指定目录下的文件及目录信息
arrDir = [fm contentsOfDirectoryAtPath:filepath error:nil];
NSLog(@"%@",arrDir);
NSFileManger 创建目录及用法
NSFileManager *fm = [NSFileManager defaultManager];
//1.创建目录
NSString *createPath = @"/Users/apple/Desktop/test1";
NSDictionary *dicInfo = [NSDictionary dictionary];
BOOL isYES = [fm createDirectoryAtPath:createPath withIntermediateDirectories:NO attributes:dicInfo error:nil];
//注意:withIntermediateDirectories这个属性是路径中间的目录如果没有是否跟着创建
if(isYES)
NSLog(@"目录创建成功!");
//2.创建文件
NSString *filepath = @"/Users/apple/Desktop/test/a.plist";
NSDictionary *dicFile = [NSDictionary dictionary];
//NSData是一个二进制数据的类,比如下载文件比较大,就需要一部分一部分下载
NSString *strContent = @"这是我要存储的内容";
NSData *data = [strContent dataUsingEncoding:NSUTF8String ];
BOOL isYES = [fm createFileAtPath:filepath contents:data attributes:dicFile ];
if(isYES)
NSLog(@"文件创建及写入成功!");
//3.copy文件
NSString *path1 = @"/Users/apple/Desktop/test/a1.plist";
NSString *path2 = @"/Users/apple/Desktop/";
BOOL isYes = [fm copyItemAtPath:path1 toPath:path2 error:nil];
if(isYES)
NSLog(@"拷贝文件成功!");
//4.移动文件
NSString *path1 = @"/Users/apple/Desktop/test/a1.plist";
NSString *path2 = @"/Users/apple/Desktop/";
BOOL isYES = [fm moveItemAtPath:path1 toPath:path2 error:nil];
if(isYES)
NSLog(@"移动文件成功");
//5.删除文件
NSString *dpath = @"/Users/apple/Desktop/test/a1.plist";
BOOL isYES = [fm deleteItemAtPath:dpath error:nil];
if(isYES)
NSLog(@"文件删除成功!");
3.NSFileManager文件下载思路
例如:更新软件,过程就是远程下载文件,然后更新文件
步骤:
1.要下载的文件在服务器
2.客服端连接服务器,通过一个地址,使用Http协议
3.返回文件大小及信息到客户端,以二进制流的形式
4.服务器把文件分解多个小文件
要求:会断点续传,就是今天下载了一点,明天在来下载,那么明天还可以继续接着下载
相关文章推荐
- Objective-C NSDate的使用
- Objective-C 常见的结构体
- Objective-C 集合对象的内存管理
- OBjective-C:在可变数组NSMutableArray中添加相同对象后,进行自定义的排序方式输出
- Objective-C Copy的概念、入门和内存管理
- Objective-C 单例模式的实现
- Objective-C:NSDectionary字典的常见操作
- Objective-C:NSMutableArray类的常见操作
- Objective-C:NSArray的常见操作
- The method find(String, Object[]) is ambiguous for the type
- OC基础回顾(十)属性
- 保证任务在主线程中执行(NSObject+GCD.h)
- objective c, property, copy
- Objective-C 学习笔记 4 数据类型和表达式
- Object-C 和 Swift 混编 之二 Swift 中调用 Object-C
- List<? extends object> 解释
- LINK - Objective-C to C#
- IOS开发系列--Objective-C之类和对象
- IOS开发系列--Objective-C之协议、代码块、分类
- IOS开发系列&mdash;Objective-C之内存管理