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)