发布于 2015-07-05 11:56:53 | 122 次阅读 | 评论: 0 | 来源: 网络整理

文件处理在 NSFileManager类的帮助下可实现大多数文件操作功能。

文件处理的方法

下面列出的方法用于访问和操作文件列表。在这里,我们必须替换, FilePath1, FilePath2 和 FilePath 我们所需的完整的文件路径字符串,以获得所需的动作。

检查一个路径文件是否存在


   NSFileManager *fileManager = [NSFileManager defaultManager];
   //Get documents directory
   NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains
   (NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0];
   if ([fileManager fileExistsAtPath:@""]==YES) {
        NSLog(@"File exists");
    }    

比较两个文件的内容


   if ([fileManager contentsEqualAtPath:@"FilePath1" andPath:@" FilePath2"]) {
      NSLog(@"Same content");
   }

检查是否可写,可读和可执行


   if ([fileManager isWritableFileAtPath:@"FilePath"]) {
      NSLog(@"isWritable");
   }
   if ([fileManager isReadableFileAtPath:@"FilePath"]) {
      NSLog(@"isReadable");
   }
   if ( [fileManager isExecutableFileAtPath:@"FilePath"]){
      NSLog(@"is Executable");
   }

移动文件


   if([fileManager moveItemAtPath:@"FilePath1" 
   toPath:@"FilePath2" error:NULL]){
      NSLog(@"Moved successfully");
   }

复制文件


   if ([fileManager copyItemAtPath:@"FilePath1" 
   toPath:@"FilePath2"  error:NULL]) {
      NSLog(@"Copied successfully");
   }

删除文件


   if ([fileManager removeItemAtPath:@"FilePath" error:NULL]) {
      NSLog(@"Removed successfully");
   }

读取文件


   NSData *data = [fileManager contentsAtPath:@"Path"];

写文件


   [fileManager createFileAtPath:@"" contents:data attributes:nil];

我们已经成功上了解到的各种文件的访问和操作技术,现在知道使用各种操作的文件和文件了吧?!

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务