Object C学习笔记25-文件管理(一)

  在此篇文章中简单记录一下文件管理,在Object C中NSFileManager用于管理文件已经路径。在Object C中的文件路径可以是相对路径也可以是绝对路径。斜线“/”开头,斜线实际上就是一个目录,称为 根目录。字符(~)用作用户主目录的缩写。点“ . ”表示当前目录,两点“  .. ”表示父目录。

  一. 创建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=501
2014-05-02 23:24:23.993 PIOFile[537:303] ====== NSFileHFSTypeCode=0
2014-05-02 23:24:23.993 PIOFile[537:303] ====== NSFileSystemFileNumber=18447915
2014-05-02 23:24:23.994 PIOFile[537:303] ====== NSFileExtensionHidden=0
2014-05-02 23:24:23.994 PIOFile[537:303] ====== NSFileSystemNumber=16777219
2014-05-02 23:24:23.995 PIOFile[537:303] ====== NSFileSize=38
2014-05-02 23:24:23.995 PIOFile[537:303] ====== NSFileGroupOwnerAccountID=0
2014-05-02 23:24:23.995 PIOFile[537:303] ====== NSFileOwnerAccountName=hechen
2014-05-02 23:24:23.997 PIOFile[537:303] ====== NSFileCreationDate=2014-05-02 14:48:12 +0000
2014-05-02 23:24:23.997 PIOFile[537:303] ====== NSFilePosixPermissions=420
2014-05-02 23:24:23.997 PIOFile[537:303] ====== NSFileHFSCreatorCode=0
2014-05-02 23:24:23.998 PIOFile[537:303] ====== NSFileType=NSFileTypeRegular
2014-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=wheel
2014-05-02 23:24:23.999 PIOFile[537:303] ====== NSFileReferenceCount=1
2014-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);
原文地址:https://www.cnblogs.com/qingyuan/p/3704591.html