iOS 获取文件的属性

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);
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容