获取文件/文件系统属性的方法----attributesOfItemAtPath:和attributesOfFileSystemForPath:

OC中获取文件/文件系统属性的方法介绍

一.什么是文件系统(FileSystem)

  • 文件系统``是操作系统用来操作文件的方法数据结构
  • 文件的存储位置
    • 存储设备(常见的是磁盘,也有基于NAND Flash的固态硬盘)
    • 分区
  • Mac的文件系统的存储大小可以在 关于本机-存储 中查看
    739332-20150918011734836-81836136.png

二.OC中获取文件/文件系统属性(Attributes)的方法

1. 获取文件属性的方法---- attributesOfItemAtPath: error:
1.1 方法介绍
  • 获得所给路径(path)的所有``文件属性(fileAttributes),返回一个字典(NSDictionary)来存储,即以键值对的形式存储
1.2 OC中的原文描述
/* attributesOfItemAtPath:error: returns an NSDictionary of key/value pairs containing the attributes of the item (file, directory, symlink, etc.) at the path in question. If this method returns 'nil', an NSError will be returned by reference in the 'error' parameter. This method does not traverse a terminal symlink.
 
    This method replaces fileAttributesAtPath:traverseLink:.
 */
- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
1.3 文件属性中包含的键
  • 返回的文件属性是以字典的形式存储的,而字典中可用的key如下:
FOUNDATION_EXPORT NSString * const NSFileType;
FOUNDATION_EXPORT NSString * const NSFileTypeDirectory;
FOUNDATION_EXPORT NSString * const NSFileTypeRegular;
FOUNDATION_EXPORT NSString * const NSFileTypeSymbolicLink;
FOUNDATION_EXPORT NSString * const NSFileTypeSocket;
FOUNDATION_EXPORT NSString * const NSFileTypeCharacterSpecial;
FOUNDATION_EXPORT NSString * const NSFileTypeBlockSpecial;
FOUNDATION_EXPORT NSString * const NSFileTypeUnknown;
FOUNDATION_EXPORT NSString * const NSFileSize;
FOUNDATION_EXPORT NSString * const NSFileModificationDate;
FOUNDATION_EXPORT NSString * const NSFileReferenceCount;
FOUNDATION_EXPORT NSString * const NSFileDeviceIdentifier;
FOUNDATION_EXPORT NSString * const NSFileOwnerAccountName;
FOUNDATION_EXPORT NSString * const NSFileGroupOwnerAccountName;
FOUNDATION_EXPORT NSString * const NSFilePosixPermissions;
FOUNDATION_EXPORT NSString * const NSFileSystemNumber;
FOUNDATION_EXPORT NSString * const NSFileSystemFileNumber;
FOUNDATION_EXPORT NSString * const NSFileExtensionHidden;
FOUNDATION_EXPORT NSString * const NSFileHFSCreatorCode;
FOUNDATION_EXPORT NSString * const NSFileHFSTypeCode;
FOUNDATION_EXPORT NSString * const NSFileImmutable;
FOUNDATION_EXPORT NSString * const NSFileAppendOnly;
FOUNDATION_EXPORT NSString * const NSFileCreationDate;
FOUNDATION_EXPORT NSString * const NSFileOwnerAccountID;
FOUNDATION_EXPORT NSString * const NSFileGroupOwnerAccountID;
FOUNDATION_EXPORT NSString * const NSFileBusy;
FOUNDATION_EXPORT NSString * const NSFileProtectionKey NS_AVAILABLE_IOS(4_0);
FOUNDATION_EXPORT NSString * const NSFileProtectionNone NS_AVAILABLE_IOS(4_0);
FOUNDATION_EXPORT NSString * const NSFileProtectionComplete NS_AVAILABLE_IOS(4_0);
FOUNDATION_EXPORT NSString * const NSFileProtectionCompleteUnlessOpen NS_AVAILABLE_IOS(5_0);
FOUNDATION_EXPORT NSString * const NSFileProtectionCompleteUntilFirstUserAuthentication NS_AVAILABLE_IOS(5_0);
  • 可以使用点语法获取的属性值
- (unsigned long long)fileSize;
- (NSDate *)fileModificationDate;
- (NSString *)fileType;
- (NSUInteger)filePosixPermissions;
- (NSString *)fileOwnerAccountName;
- (NSString *)fileGroupOwnerAccountName;
- (NSInteger)fileSystemNumber;
- (NSUInteger)fileSystemFileNumber;
- (BOOL)fileExtensionHidden;
- (OSType)fileHFSCreatorCode;
- (OSType)fileHFSTypeCode;
- (BOOL)fileIsImmutable;
- (BOOL)fileIsAppendOnly;
- (NSDate *)fileCreationDate;
- (NSNumber *)fileOwnerAccountID;
- (NSNumber *)fileGroupOwnerAccountID;
1.4 使用实例
//设置一个路径(以自己桌面上的Test文件夹为例)
NSString *filePath = @"/Users/LN/Desktop/Test";

//获得所给文件路径的文件属性
NSDictionary *attr = [fm attributesOfItemAtPath:filePath error:nil];

//通过键在返回的文件属性中获取文件的大小
//NSInteger fileSize = [attrs[NSFileSize] integerValue];
//通过点语法在返回的文件属性中获取文件的大小        
NSInteger fileSize = attrs.fileSize;
        
//打印这个文件的大小(在Mac上查看文件大小:选中+空格)
NSLog(@"%zd",fileSize);
2. 获取文件系统属性的方法----attributesOfFileSystemForPath:error:
1.1 方法介绍
  • 获得所给路径(path)所在``文件系统的属性,返回一个字典(NSDictionary)来存储,即以键值对的形式存储
1.2 OC中的原文描述
/* attributesOfFileSystemForPath:error: returns an NSDictionary of key/value pairs containing the attributes of the filesystem containing the provided path. If this method returns 'nil', an NSError will be returned by reference in the 'error' parameter. This method does not traverse a terminal symlink.
 
    This method replaces fileSystemAttributesAtPath:.
 */
- (NSDictionary *)attributesOfFileSystemForPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
1.3 文件属性中包含的键
  • 返回的文件属性是以字典的形式存储的,而字典中可用的key如下:
FOUNDATION_EXPORT NSString * const NSFileSystemSize;
FOUNDATION_EXPORT NSString * const NSFileSystemFreeSize;
FOUNDATION_EXPORT NSString * const NSFileSystemNodes;
FOUNDATION_EXPORT NSString * const NSFileSystemFreeNodes;
1.4 使用实例--打印NSFileSystemSize和NSFileSystemFreeSize
  • NSFileSystemSize(获得磁盘实际存储大小)
  • NSFileSystemSize(获得磁盘实际剩余存储大小)
//设置一个路径(以自己桌面上的Test文件夹为例)
NSString *filePath = @"/Users/LN/Desktop/Test";

//获得所给文件路径所在文件系统的属性
NSDictionary *attrs = [fm attributesOfFileSystemForPath:file error:nil];

//取出文件系统的属性中NSFileSystemSize键所对应的值,即系统硬盘已经存储的大小
 NSNumber *systemSize = attrs[NSFileSystemSize];
 NSNumber *systemFreeSize = attrs[NSFileSystemFreeSize];       

//打印这个文件的大小(在Mac上查看文件大小:选中+空格)
NSLog(@"%@", systemSize); 
NSLog(@"%@", systemFreeSize); 
打印结果1--NSFileSystemSize(以b位单位,1000b == 1kb)

739332-20150918011810554-1818062357.png

739332-20150918011834211-683753261.png

打印结果2--NSFileSystemFreeSize(以b位单位,1000b == 1kb)

739332-20150918012143992-296099426.png

739332-20150918011924367-952213462.png

转载于:https://www.cnblogs.com/KrystalNa/p/4818100.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值