iOS文件和目录操作,iOS文件操作,NSFileManager使用文件操作:

NSFileManager常用的文件方法:

-(NSData*)contentsAtPath:path 从一个文件中读取数据

-(BOLL)createFileAtPath:path contents:(NSData*)data attributes: attr 向一个文件写入数据

-(BOOL)removeItemAtPath:path error:err 删除一个文件

-(BOOL)moveItemAtPath:from toPath:to error:err 重命名或移动一个文件(to 不能是已存在的)

-(BOOL)copyItemAtPath:from toPath:to error:err 复制文件(to 不能是已存在的)

-(BOOL)contentsEqualAtPath:path1 andPath:path2 比较这两个文件的内容

-(BOOL)fileExistsAtPath:path 测试文件是否存在

-(BOOL)isReadableFileAtPath:path 测试文件是否存在,并且是否能执行读操作

-(BOOL)isWritableFileAtPath:path 测试文件是否存在,并且是否能执行写操作

-(NSDictionary*)attributesOfItemAtPath:path error:err 获取文件的属性

属性字典允许你指定要创建的文件的权限,如果将该参数指定为nil,该文件会被设置为默认权限。

 

1、通过一段程序来对文件进行操作:

 

  1. //  
  2. //  main.m  
  3. //  NSFileManager_01  
  4. //  
  5. //  Created by swinglife on 13-11-10.  
  6. //  Copyright (c) 2013年 swinglife. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. int main(int argc, const char * argv[])  
  12. {  
  13.       
  14.     @autoreleasepool {  
  15.         //文件名  
  16.         NSString *fileName = @"testFile";  
  17.         NSString *fileContent = @"这是文件内容!!!!";  
  18.         NSData *fileData = [fileContent dataUsingEncoding:NSUTF8StringEncoding];  
  19.           
  20.         //创建NSFileManager实例  
  21.         NSFileManager *fm = [NSFileManager defaultManager];  
  22.           
  23.         //创建文件  
  24.         [fm createFileAtPath:fileName contents:fileData attributes:nil];  
  25.           
  26.         //判断文件是否存在 不存在就结束程序  
  27.         if([fm fileExistsAtPath:fileName]==NO){  
  28.             NSLog(@"文件不存在");  
  29.             return 1;  
  30.         }  
  31.           
  32.         //拷贝文件  
  33.         if([fm copyItemAtPath:fileName toPath:@"newFile" error:NULL]==NO){  
  34.             NSLog(@"复制失败");  
  35.             return 2;  
  36.         }  
  37.           
  38.         //测试两个文件是否相同  
  39.         if([fm contentsEqualAtPath:fileName andPath:@"newFile"]==NO){  
  40.             NSLog(@"文件不相同");  
  41.             return 3;  
  42.         }  
  43.           
  44.         //重命名newFile  
  45.         [fm moveItemAtPath:@"newFile" toPath:@"newFile2" error:NULL];  
  46.           
  47.         //获取newFile2的大小  
  48.         NSDictionary *fileAttr = [fm attributesOfItemAtPath:@"newFile2" error:NULL];  
  49.         if(fileAttr!=nil){  
  50.             NSLog(@"文件大小:%llu bytes",[[fileAttr objectForKey:NSFileSize] unsignedLongLongValue]);  
  51.         }  
  52.           
  53.         //删除文件  
  54.         [fm removeItemAtPath:fileName error:NULL];  
  55.           
  56.         //显示newFile2的内容  
  57.         NSString *data = [NSString stringWithContentsOfFile:@"newFile2" encoding:NSUTF8StringEncoding error:NULL];  
  58.         NSLog(@"%@",data);  
  59.           
  60.           
  61.     }  
  62.     return 0;  
  63. }  

 

 

NSFileManager常用的目录方法


-(NSString*)currentDirectoryPath 获取当前目录

-(BOOL)changeCurrentDirectoryPath:path 更改当前目录

-(BOOL)copyItemAtPath:from toPath:to error:err 复制目录结构

-(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attributes:attr 创建一个新目录

-(BOOL)fileExistsAtPath:path isDirectory:(BOOL*)flag 测试文件是不是目录(flag中存储结果)

-(NSArray*)contentsOfDirectoryAtPath:path error:err 列出目录内容

-(NSDirectoryEnumerator*)enumeratorAtPath:path 枚举目录的内容

-(BOOL)removeItemAtPath:path error:err 删除空目录

-(BOOL)moveItemAtPath:from toPath:to error:err 重命名或移动一个目录

 

2、通过一段程序来对目录进行操作:

 

    1. //  
    2. //  main.m  
    3. //  NSFileManager_02  
    4. //  
    5. //  Created by swinglife on 13-11-10.  
    6. //  Copyright (c) 2013年 swinglife. All rights reserved.  
    7. //  
    8.   
    9. #import <Foundation/Foundation.h>  
    10.   
    11. int main(int argc, const char * argv[])  
    12. {  
    13.   
    14.     @autoreleasepool {  
    15.         //文件目录  
    16.         NSString *dirName = @"testDir";  
    17.           
    18.         //创建NSFileManager实例  
    19.         NSFileManager *fm = [NSFileManager defaultManager];  
    20.           
    21.         //获取当前目录  
    22.         NSString *path = [fm currentDirectoryPath];  
    23.         NSLog(@"Path:%@",path);  
    24.           
    25.         //创建新目录  
    26.         [fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:NULL];  
    27.           
    28.         //重命名新的目录  
    29.         [fm moveItemAtPath:dirName toPath:@"newDir" error:NULL];  
    30.           
    31.         //更改当前目录到新的目录  
    32.         [fm changeCurrentDirectoryPath:@"newDir"];  
    33.           
    34.         //获取当前工作目录  
    35.         path = [fm currentDirectoryPath];  
    36.         NSLog(@"Path:%@",path);  
    37.           
    38.     }  
    39.     return 0;  
    40. }  
posted @ 2017-04-25 16:33  孙富有(iOS工程师)  阅读(209)  评论(0编辑  收藏  举报