天天看点

iPhone开发中现文件的增加 删除和查询

iPhone开发中,我们常常用到一些对于文件的增加,删除和查询,这些基本的功能对于开发者来说非常的重要,而且非常实用。本文给大家介绍一下如何实现这几个具体的功能。

  //创建文件

  -(void)CreateFile:(NSString*)path fileName:(NSString*)filename

  {

  //创建文件管理器

  NSFileManager *fileManager = [NSFileManager defaultManager];

  //更改到待操作的目录下

  [fileManager changeCurrentDirectoryPath:[path stringByExpandingTildeInPath]];

  //创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil

  [fileManager createFileAtPath:filename contents:nilattributes:nil];

  }

  //删除文件

  -(BOOL)DeleteFile:(NSString*)path

  @try {

  //删除

  [fileManager removeItemAtPath:path error:nil];

  return YES;

  @catch (NSException *exception) {

  return NO;

  @finally {

  //获取某文件夹下的所有文件

  -(NSArray*)GetFilesName:(NSString*)path

  NSArray *files = [fileManager subpathsAtPath: path ];

  return files;

本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/wws5201985/811894,如需转载请自行联系原作者

继续阅读