文件属性及操作

来源:互联网 发布:城市新闻网络交换平台 编辑:程序博客网 时间:2024/06/07 18:30
  在应用程序执行时,经常需要本地化保存一些重要的数据,这时就有可能需要创建一些目录。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字典中的不同键的值,这个接口所创建的目录的属性就会基本满足我们的需求了。


一. 创建NSFileManager 对象

    NSFileManager很是简单,可以应用如式格式来创建NSFileManager对象。



NSString* fileName=[[NSString alloc] initWithFormat:@"/ISO DeV/File.txt"];NSFileManager *fileManager=nil;fileManager=[NSFileManager defaultManager];


 


  二. 断定文件是否存在


    应用fileExistsAtPath断定某个文件是否存在,上方已经所过了,可以应用绝对路径 也可以应用相对路径



if([fileManager fileExistsAtPath:fileName]==YES){     NSLog(@"该文件存在");}else{     NSLog(@"该文件不存在");}


 


  三. 拷贝文件


    应用函数copyPath:(NSString*) toPath(NSString*) 来拷贝一个文件,拷贝文件可以从头定名一个文件名称



NSString *toFileName=@"/ISO DeV/Test/File1.txt";        NSLog(@"%d",[fileManager fileExistsAtPath:toFileName]);        [fileManager copyPath:fileName toPath:toFileName handler:nil];        if([fileManager fileExistsAtPath:toFileName]==YES){            NSLog(@"该文件存在");        }else{            NSLog(@"该文件不存在");}


 


  四. 断定文件内容是否相等



if([fileManager contentsEqualAtPath:fileName andPath:toFileName]==YES){            NSLog(@"文件内容雷同");        }else{            NSLog(@"文件内容不一样");}


 


  五. 重定名文件



NSString *newFileName=@"/ISO DeV/Test/File2.txt";[fileManager movePath:toFileName toPath:newFileName handler:nil];


 


  六. 获得文件属性



NSDictionary *dic= [fileManager fileAttributesAtPath:newFileName traverseLink:NO];        for (NSString *key in[dic keyEnumerator]) {                                    NSLog(@"====== %@=%@",key,[dic valueForKey:key]);        }


  应用办法fileAttributesAtPath 获得某个路径下的文件的属性,返回值是一个NSDictionary. 以上代码输出获得如下:



2014-05-02 23:24:23.993 PIOFile[537:303] ====== NSFileOwnerAccountID=5012014-05-02 23:24:23.993 PIOFile[537:303] ====== NSFileHFSTypeCode=02014-05-02 23:24:23.993 PIOFile[537:303] ====== NSFileSystemFileNumber=184479152014-05-02 23:24:23.994 PIOFile[537:303] ====== NSFileExtensionHidden=02014-05-02 23:24:23.994 PIOFile[537:303] ====== NSFileSystemNumber=167772192014-05-02 23:24:23.995 PIOFile[537:303] ====== NSFileSize=382014-05-02 23:24:23.995 PIOFile[537:303] ====== NSFileGroupOwnerAccountID=02014-05-02 23:24:23.995 PIOFile[537:303] ====== NSFileOwnerAccountName=hechen2014-05-02 23:24:23.997 PIOFile[537:303] ====== NSFileCreationDate=2014-05-02 14:48:12 +00002014-05-02 23:24:23.997 PIOFile[537:303] ====== NSFilePosixPermissions=4202014-05-02 23:24:23.997 PIOFile[537:303] ====== NSFileHFSCreatorCode=02014-05-02 23:24:23.998 PIOFile[537:303] ====== NSFileType=NSFileTypeRegular2014-05-02 23:24:23.998 PIOFile[537:303] ====== NSFileExtendedAttributes={    "com.apple.TextEncoding" = <7574662d 383b3133 34323137 393834>;}2014-05-02 23:24:23.999 PIOFile[537:303] ====== NSFileGroupOwnerAccountName=wheel2014-05-02 23:24:23.999 PIOFile[537:303] ====== NSFileReferenceCount=12014-05-02 23:24:24.000 PIOFile[537:303] ====== NSFileModificationDate=2014-05-02 15:12:27 +0000


 


  七. 删除文件



[fileManager removeFileAtPath:newFileName handler:nil];


  经由过程办法removeFileAtPath 可以删除文件


 


  八. 获取文件内容



NSString *content=[NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];        NSLog(@"%@",content);

0 0