您的位置:首页 > 移动开发 > Objective-C

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.服务器把文件分解多个小文件

要求:会断点续传,就是今天下载了一点,明天在来下载,那么明天还可以继续接着下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: