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;
}