NSFileManager文件操作:
| -(NSData *) contentsAtPath: path | 从一个文件中读取数据 | 
| -(bool)createFileAtPath:path contents:(NSData *)data attributes:attr | 创建一个文件并写入数据 | 
| -(BOOL) removeItemAtPath: path error: err | 删除一个文件 | 
| -(BOOL) moveItemAtPath: from toPath: to error: err | 重命名或移动一个文件 | 
| -(BOOL) copyItemAtPath: from toPath: to error: err | 复制文件(目标存在会复制失败) | 
| -(BOOL) contentsEqualAtPath: path1 andPath: path2 | 比较两个文件的内容 | 
| -(BOOL) fileExistsAtPath: path | 测试文件是否存在 | 
| -(BOOL) isReadableFileAtPath: path | 测试文件是否存在,并且能否读操作 | 
| -(BOOL) isWritableFileAtPath: path | 测试文件是否存在,并且能否写操作 | 
| -(NSDicSonary *) aFributesOfItemAtPath: path error: err | 获取文件的属性 | 
| -(BOOL) setAFributesOfItemAtPath: aFr error: err | 更改文件的属性 | 
1. 获得当前目录
     // 创建文件操作对象 fm
        NSFileManager *fm=[NSFileManager defaultManager];
        // 得到当前目录信息
        NSLog(@"当前目录=%@",[fm currentDirectoryPath]);
2. 判断文件是否存在
        // 创建文件操作对象 fm
        NSFileManager *fm=[NSFileManager defaultManager];
        // 判断文件是否存在
        NSString *fPathName=@"/Users/haiyefeng/Desktop/text1.txt"; // 不区分大小写
        if([fm fileExistsAtPath:fPathName]==NO)
        {
            NSLog(@"文件(%@)不存在!",fPathName);
            return 1;
        }
        else
        {
            NSLog(@"存在");
        }
3. 复制文件
        // 创建文件操作对象 fm
        NSFileManager *fm=[NSFileManager defaultManager];
        // 复制文件
        NSString *error1;  // 创建一个字符串存放错误信息
        NSString *fPathName=@"/Users/haiyefeng/Desktop/text3.txt";
        NSString *fPathName2=@"/Users/haiyefeng/Desktop/text5.txt";  // 创建一个路径信息
        if([fm copyItemAtPath:fPathName toPath:fPathName2 error:&error1]==NO)
        {
            NSLog(@"复制文件失败!");
            return 2;
        }
        else
        {
            NSLog(@"文件创建成功!");
        }
4. 判断文件内容是否相等
      NSFileManager *fm=[NSFileManager defaultManager];
        NSString *fPathName=@"/Users/haiyefeng/Desktop/text3.txt";
        NSString *fPathName4=@"/Users/haiyefeng/Desktop/text5.txt";
        if([fm contentsEqualAtPath:fPathName andPath:fPathName4]==NO)
        {
            NSLog(@"文件不相等!");
            return 3;
        }
        else
        {
            NSLog(@"文件相等!");
        }
5. 重命名文件
       NSFileManager *fm=[NSFileManager defaultManager];
        NSString * fPathName3=@"/Users/haiyefeng/Desktop/text4.txt";
        NSString * fPathName=@"/Users/haiyefeng/Desktop/text3.txt";
        NSString *error3;
        if([fm moveItemAtPath:fPathName toPath:fPathName3 error:&error3]==NO)
        {
            NSLog(@"文件重命名失败!");
            return 4;
        }
        else
        {
            NSLog(@"文件重命名成功!");
        }
6. 删除文件
        NSFileManager *fm=[NSFileManager defaultManager];
        NSString * fPathName2=@"/Users/haiyefeng/Desktop/text4.txt";
        if([fm removeItemAtPath:fPathName2 error:NULL]==NO)
        {
            NSLog(@"文件删除失败");
            return 0;
        }
        else
        {
            NSLog(@"操作成功");
        }
 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号