attributesOfItemAtPath:方法的使用

    attributesOfItemAtPath:方法的使用


    attributesOfItemAtPath:方法的功能是获取文件的大小、文件的内容等属性,其语法形式如下:

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

    其中,(NSString *)path用来指定要或者属性的文件;error(NSError *_autoreleasing *)error用来指定错误。
    【示例】以下程序通过使用attributesOfItemAtPath:方法获取桌面上的副本111文件的属性。程序代码如下:

    #import
    int main(int argc, const char * argv[])
    {
    @autoreleasepool {
    NSString *path1=@"/Users/hehehe/Desktop/副本111";
    NSFileManager *manager=[NSFileManager defaultManager];
    NSLog(@"%@",[manager attributesOfItemAtPath:path1 error:nil]);//获取文件的属性
    }
    return 0;
    }


    运行结果如下:

    2013-03-25 22:05:26.140 8-5[1586:303] {
    NSFileCreationDate = "2013-03-26 03:03:26 +0000";
    NSFileExtendedAttributes =     {
    "com.apple.TextEncoding" = <7574662d 383b3133 34323137 393834>;
    };
    NSFileExtensionHidden = 0;
    NSFileGroupOwnerAccountID = 20;
    NSFileGroupOwnerAccountName = staff;
    NSFileHFSCreatorCode = 0;
    NSFileHFSTypeCode = 0;
    NSFileModificationDate = "2013-03-26 03:03:58 +0000";
    NSFileOwnerAccountID = 501;
    NSFileOwnerAccountName = hehehe;
    NSFilePosixPermissions = 420;
    NSFileReferenceCount = 1;
    NSFileSize = 19;
    NSFileSystemFileNumber = 3145017;
    NSFileSystemNumber = 16777225;
    NSFileType = NSFileTypeRegular;
    }


    关键字:attributesOfItemAtPath:方法   获取文件的属性   Objective-C


工作中例子(获取文件的大小):

+ (unsigned long long int)getFileSize:(NSString *)filePath{

    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];

    unsigned long long  int length = [fileAttributes fileSize];


    return length;

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值