OC中获取文件/文件系统属性的方法介绍 一.什么是文件系统(FileSystem) 文件系统``是操作系统用来操作文件的方法和数据结构 文件的存储位置 存储设备(常见的是磁盘,也有基于NAND Flash的固态硬盘) 分区 Mac的文件系统的存储大小可以在 关于本机-存储 中查看
二.OC中获取文件/文件系统属性(Attributes)的方法
获取文件属性的方法---- 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);
获取文件系统属性的方法----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)
打印结果2--NSFileSystemFreeSize(以b位单位,1000b == 1kb)
网友评论