文件管理类NSFileManager常用操作

http://blog.sina.com.cn/s/blog_9cd1705d01011gel.html

iphone创建文件

注意2点:

1、创建多级目录的文件时,要先判断其目录是否存在,如果不存在就创建该目录,如果没有创建该目录,文件是不能创建成功的

2、不要使用- (BOOL)createDirectoryAtPath:(NSString *)path attributes:(NSDictionary *)attributes,这个方法在模拟器中可能能成功运行,但在设备上肯定不行的,

改用- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error

记得将createIntermediates设置为YES,这样就能建立多级目录了。如果是一级目录,可以设置为NO


demo如下:



   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);


   NSString *documentsDirectory = [paths objectAtIndex:0];

   NSString *strFile = [documentsDirectory stringByAppendingPathComponent:@"hello/config.plist"];

   NSLog(@"strFile: %@", strFile);


   NSString *strPath = [documentsDirectory stringByAppendingPathComponent:@"hello"];

   if (![[NSFileManager defaultManager] fileExistsAtPath:strPath]) {

       NSLog(@"there is no Directory: %@",strPath);

       [[NSFileManager defaultManager] createDirectoryAtPath:strPath withIntermediateDirectories:YES attributes:nil error:nil];

   }


   NSArray *keys = [NSArray arrayWithObjects: @"username", @"password", @"serverName", @"serverPort", @"autoSave", nil];

   NSArray *values = [NSArray arrayWithObjects: @"11", @"11", @"11", @"11", @"11", nil];


   NSDictionary *configInfo = [[NSDictionary alloc] initWithObjects: values forKeys:keys];

   if (![configInfo writeToFile: strFile atomically: YES]) {

       NSLog(@"write file error");

   }


http://www.cnblogs.com/ydhliphonedev/archive/2012/11/18/2776588.html

在应用程序执行时,经常需要本地化保存一些重要的数据,这时就有可能需要创建一些目录。Objective-C提供了一个非常强大的创建目录的接口:

- ( BOOL )createDirectoryAtPath:( NSString  *)path withIntermediateDirectories:( BOOL )createIntermediates attributes:( NSDictionary  *)attributes error:( NSError  **)error;

   很多人使用这个接口时,往往将attributes参数设置为nil,这样虽然能够创建出目录,但是在一些特殊场景下(比如iPhone的apps)所创建的目录的属性往往不是我们期望的,因而导致目录的读写失败等问题。其实通过设置attributes参数,这个接口可以完成我们的期望。

      根据苹果官方文档介绍,这个参数可以设置所创建目录所属的用户和用户组,目录的访问权限和修改时间等。如果设置为nil,那么所创建目录的属性则采用系统默认设置,一般会将目录的用户设置为root,访问权限设置为0755,这样就导致其他用户向这个目录写入时失败。

      attributes参数是一个字典类型。查看苹果官方文档的介绍,可以看到在NSFileManager.h头文件定义了很多常量字符串,用于作为attributes字典的键,针对于这个接口的键主要包括下面几个:

NSString  * const  NSFileType ;
NSString  * const  NSFileSize ;
NSString  * const  NSFileModificationDate ;
NSString  * const  NSFileReferenceCount ;
NSString  * const  NSFileDeviceIdentifier ;
NSString  * const  NSFileOwnerAccountName ;
NSString  * const  NSFileGroupOwnerAccountName ;
NSString  * const  NSFilePosixPermissions ;
NSString  * const  NSFileSystemNumber ;
NSString  * const  NSFileSystemFileNumber ;
NSString  * const  NSFileExtensionHidden ;
NSString  * const  NSFileHFSCreatorCode ;
NSString  * const  NSFileHFSTypeCode ;
NSString  * const  NSFileImmutable ;
NSString  * const  NSFileAppendOnly ;
NSString  * const  NSFileCreationDate ;
NSString  * const  NSFileOwnerAccountID ;
NSString  * const  NSFileGroupOwnerAccountID ;
NSString  * const  NSFileBusy ;

      本文不打算翻译苹果的官方文档,只把我们比较关心的几个键的意义说明如下:

  • NSFileAppendOnly

      这个键的值需要设置为一个表示布尔值的NSNumber对象,表示创建的目录是否是只读的。

  • NSFileCreationDate

      这个键的值需要设置为一个NSDate对象,表示目录的创建时间。

  • NSFileOwnerAccountName

      这个键的值需要设置为一个NSString对象,表示这个目录的所有者的名字。

  • NSFileGroupOwnerAccountName

      这个键的值需要设置为一个NSString对象,表示这个目录的用户组的名字。

  • NSFileGroupOwnerAccountID

      这个键的值需要设置为一个表示unsigned int的NSNumber对象,表示目录的组ID。

  • NSFileModificationDate

      这个键的值需要设置一个NSDate对象,表示目录的修改时间。

  • NSFileOwnerAccountID

      这个键的值需要设置为一个表示unsigned int的NSNumber对象,表示目录的所有者ID。

  • NSFilePosixPermissions

      这个键的值需要设置为一个表示short int的NSNumber对象,表示目录的访问权限。

  • NSFileReferenceCount

      这个键的值需要设置为一个表示unsigned long的NSNumber对象,表示目录的引用计数,即这个目录的硬链接数。

      这样,通过合理的设计attributes字典中的不同键的值,这个接口所创建的目录的属性就会基本满足我们的需求了。

http://www.open-open.com/lib/view/open1428650503546.html

1、创建一个文件管理器

NSFileManager *fm = [NSFileManager defaultManager];

2、浅度遍历目录

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error

3、深度遍历目录

- (NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error

4、获取当前目录

- (NSString *)currentDirectoryPath

5、更改当前目录

- (BOOL)changeCurrentDirectoryPath:(NSString *)path

6、枚举目录内容

- (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path

7、创建目录

?
1
- ( BOOL )createDirectoryAtPath:(NSString *)path withIntermediateDirectories:( BOOL )createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error

8、创建文件

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

9、复制文件

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

10、删除文件

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

11、目录/文件拷贝

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

12、移动/重命名文件或者目录

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

13、测试文件是否存在

- (BOOL)fileExistsAtPath:(NSString *)path

14、获取文件信息(属性和权限)

- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error

15、从文件中读取数据

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

16、比较两个文件的内容

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

17、测试文件是否存在,且是否能执行读操作

- (BOOL)isReadableFileAtPath:(NSString *)path

18、测试文件是否存在,且是否能执行写操作

- (BOOL)isWritableFileAtPath:(NSString *)path

二、文件操作类NSFileHandle常用操作:

1、只读方式打开文件

+ (id)fileHandleForReadingAtPath:(NSString *)path

2、只写方式打开文件

+ (id)fileHandleForWritingAtPath:(NSString *)path

3、读写方式打开文件

+ (id)fileHandleForUpdatingAtPath:(NSString *)path

4、从文件当前位置读到结尾

- (NSData *)readDataToEndOfFile

5、从文件当前位置读固定字节数的内容

- (NSData *)readDataOfLength:(NSUInteger)length

6、返回所有可用的数据

- (NSData *)availableData

7、写文件

- (void)writeData:(NSData *)data

8、定位到文件尾部

- (unsigned long long)seekToEndOfFile

9、定位到文件指定位置

- (void)seekToFileOffset:(unsigned long long)offset

10、获取当前文件的偏移量

- (unsigned long long)offsetInFile

11、将文件的长度设置为offset字节

- (void)truncateFileAtOffset:(unsigned long long)offset

 关闭文件

- (void)closeFile

P.S. (网络socket中)通过initWithFileDescriptor初始化的对象,需要显式调用此方法;其它方法创建的对象会自动打开文件,该对象被销毁时会自动关闭该方法,不需显式调用此方法。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值