随笔-IOS沙盒 操作

一、IOS沙盒下获取文件路径的方式,常用的2种:

1、NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  NSString *documentsDirectory = [paths objectAtIndex:0];   
2、NSString* docPath = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];

二、IOS下沙盒下创建文件

1、NSString *filePath =[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/newfile.txt"];
2、NSString *filePath= [documentsDirectory stringByAppendingPathComponent:@"newfile.txt"];
结果为:/Documents/newfile.txt 

三、IOS沙盒下创建目录

   NSFileManager *fileManager = [NSFileManager defaultManager]; 
   NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];  
   // 创建目录   
   [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil]; 

四、IOS沙盒写入文件
1、可以用 NSFileManager的下列方法:  
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;  
2、 NSData  
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;  (常用)
- (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;
五、IOS删除文件

用 NSFileManager方法[fileMgr removeItemAtPath:filePath2 error:&error]
//Removes the file or directory at the specified URL.  
 (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error

六、IOS沙盒读取文件

NSData *data = [fileManger contentsAtPath:myFilePath];//myFilePath是包含完整路径的文件名  
或直接用NSData 的类方法:
NSData *data = [NSData dataWithContentOfPath:myFilePath];









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值