美文网首页
iOS 获取文件的属性

iOS 获取文件的属性

作者: MdWhat | 来源:发表于2018-03-16 11:36 被阅读11次

    NSString *path = [NSString stringWithFormat:@"%@/Documents/",NSHomeDirectory()];
    NSError *error = nil;
    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
    if (fileAttributes && !error) {
    NSNumber *fileSize;
    NSString *fileOwner;
    NSDate *fileModDate, *creationDate;
    //文件大小
    if ((fileSize = [fileAttributes objectForKey:NSFileSize])) {
    NSLog(@"文件大小 : %llu", [fileSize unsignedLongLongValue]);
    }
    //文件创建日期
    if ((creationDate = [fileAttributes objectForKey:NSFileCreationDate])) {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *newString = [format stringFromDate:creationDate];
    NSLog(@"文件创建时间 : %@", newString);
    }

        //文件所有者
        if ((fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName])) {
            NSLog(@"文件所有者   : %@", fileOwner);
        }
        
        //文件修改日期
        if ((fileModDate = [fileAttributes objectForKey:NSFileModificationDate])) {
            NSDateFormatter *format = [[NSDateFormatter alloc] init];
            format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            NSString *newString = [format stringFromDate:fileModDate];
            NSLog(@"文件修改时间 : %@", newString);
        }
    }

    相关文章

      网友评论

          本文标题:iOS 获取文件的属性

          本文链接:https://www.haomeiwen.com/subject/twplqftx.html