NSFileManager 文件管理

NSFileManager

创建一个文件并写入数据

- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;

从一个文件中读取数据

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

scrPath路径上的文件移动到dstPath路径上,注意这里的路径是文件路径而不是目录

 - (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **) error;

scrPath路径上的文件复制到dstPath路径上

- (BOOL)copyItemAtPath:(NSString *)scrPath toPath:(NSString *)dstPath error:(NSError **) error;

比较两个文件的内容是否一样

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

文件时候存在

 - (BOOL)fileExistsAtPath:(NSString *)path;

移除文件

 - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **) error;

创建文件管理

NSFileManager *fileManager = [NSFileManager defaultManager];    
NSString *path = [NSHomeDirectory( )  stringByAppendingPathComponent:@"holyBible.txt"];                                                                                                         
NSString *text = @"abcdefg"; 

将字符串转成NSData类型

NSData *data = [text dataUsingEncoding: NSUTF8StringEncoding]; 

写入文件

BOOL success = [fileManager createFileAtPath:path contents:data attributes:nil];

创建文件夹

NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];  
NSString *contect = @"abcdefg";                                                                         
BOOL success = [fm createFileAtPath:filePath contents:[content dataUsingEncoding: NSUTF8StringEncoding] attributes:nil];

NSFileManager-读取内容

  NSData *fileData = [fileManager contentsAtPath:filePath];            
  NSString *content = [[NSString alloc] initWithData:fileData dataUsingEncoding: NSUTF8StringEncoding];

NSData-读取内容

NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];  
NSData *data = [NSData dataWithContentOfFile:filePath];

NSString-读取内容

NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];     
NSString *content = [[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

移动、复制文件

移动文件(重命名)

 NSString *toPath = [NSHomeDirectory( ) stringByAppendingPathComponent:@"hellogod/New Testament.txt"];                                                                                  
[fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];                                                       
NSError *error;                                                                                             
BOOL isSuccess = [fm moveItemAtPath:filePath toPath:toPath error:&error];

复制文件(重命名)

NSString *copyPath = [NSHomeDirectory( ) stringByAppendingPathComponent:@"备份/Old Testament.txt"];                                                                                      
[fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];                                                       
BOOL success = [fm copyItemAtPath:toPath toPath:toPath error:nil];

删除文件、获取文件大小

判断文件是否存在和删除文件

 if([fm fileExistsAtPath])  {                                                                                                                  
      if ([fm removeItemAtPath:copyPath])     {             
                 NSLog(@"remove success"); 
     }    
 }

获取文件大小

 NSFileManager *fileManager = [NSFileManager defaultManager];                         

获得文件的属性字典

NSDictionary *attrDic = [fileManager attributesOfItemAtpath:sourcePath error:nil]; 
 NSNumber *fileSize = [attrDic objectForKey:NSFileSize];   

获取目录文件信息

NSFileManager *fileManager = [NSFileManager defaultManager];                         
NSString *enuPath = [NSHomeDirectoty( ) stringByAppendingPathComponent:@"Test"];                                                                                                           
NSDictionaryEnumerator *dirEnum = [fileManager enumeratorAtPath:enuPath];     
NSString *path = nil;                                                                                      
while ((path = [dirEnum nextObject]} != nil)   {                                      
         NSLog(@"%@",path);  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值