ios提供了一些API用于獲取和修改文件屬性。本文將詳細討論ios中如何獲取與修改文件屬性。
每個文件都有相應的屬性。這些屬性可能包含文件大小,文件修改日期,文件權限…我們可以獲取這些屬性,也可以修改某些屬性。
文件的基本結構 
通過NSFileManager的attributesOfItemAtPath方法可以獲取文件的屬性。 參數path指定文件路徑,參數error在獲取出錯時,顯示出錯信息。 若獲取失敗則返回nil,出錯信息保存在error中。若獲取成功則返回NSDictionary< NSFileAttributeKey, id>對象。文件的所有屬性都保存在NSDictionary< NSFileAttributeKey, id>對象中。
* <p> * The dictionary keys for attributes are - * </p> * <deflist> * <term><code>NSFileAppendOnly</code></term> * <desc>NSNumber ... boolean</desc> * <term><code>NSFileCreationDate</code></term> * <desc>NSDate when the file was created (if supported)</desc> * <term><code>NSFileDeviceIdentifier</code></term> * <desc>NSNumber (identifies the device on which the file is stored)</desc> * <term><code>NSFileExtensionHidden</code></term> * <desc>NSNumber ... boolean</desc> * <term><code>NSFileGroupOwnerAccountName</code></term> * <desc>NSString name of the file group</desc> * <term><code>NSFileGroupOwnerAccountID</code></term> * <desc>NSNumber ID of the file group</desc> * <term><code>NSFileHFSCreatorCode</code></term> * <desc>NSNumber not used</desc> * <term><code>NSFileHFSTypeCode</code></term> * <desc>NSNumber not used</desc> * <term><code>NSFileImmutable</code></term> * <desc>NSNumber ... boolean</desc> * <term><code>NSFileModificationDate</code></term> * <desc>NSDate when the file was last modified</desc> * <term><code>NSFileOwnerAccountName</code></term> * <desc>NSString name of the file owner</desc> * <term><code>NSFileOwnerAccountID</code></term> * <desc>NSNumber ID of the file owner</desc> * <term><code>NSFilePosixPermissions</code></term> * <desc>NSNumber posix access permissions mask</desc> * <term><code>NSFileReferenceCount</code></term> * <desc>NSNumber number of links to this file</desc> * <term><code>NSFileSize</code></term> * <desc>NSNumber size of the file in bytes</desc> * <term><code>NSFileSystemFileNumber</code></term> * <desc>NSNumber the identifier for the file on the filesystem</desc> * <term><code>NSFileSystemNumber</code></term> * <desc>NSNumber the filesystem on which the file is stored</desc> * <term><code>NSFileType</code></term> * <desc>NSString the type of file</desc> * </deflist>ios為NSDictionary< NSFileAttributeKey, id>添加了一些方法用于獲取屬性值。
@interface NSDictionary<KeyType, ObjectType> (NSFileAttributes)- (unsigned long long)fileSize;- (nullable NSDate *)fileModificationDate;- (nullable NSString *)fileType;- (NSUInteger)filePosixPermissions;- (nullable NSString *)fileOwnerAccountName;- (nullable NSString *)fileGroupOwnerAccountName;- (NSInteger)fileSystemNumber;- (NSUInteger)fileSystemFileNumber;- (BOOL)fileExtensionHidden;- (OSType)fileHFSCreatorCode;- (OSType)fileHFSTypeCode;- (BOOL)fileIsImmutable;- (BOOL)fileIsAppendOnly;- (nullable NSDate *)fileCreationDate;- (nullable NSNumber *)fileOwnerAccountID;- (nullable NSNumber *)fileGroupOwnerAccountID;@end例如:
NSString *dir=NSHomeDirectory();NSString *file_name=[dir stringByAppendingPathComponent:@"aaa"];NSFileManager *manager=[NSFileManager defaultManager];NSError *error=nil;NSDictionary *dict=[manager attributesOfItemAtPath:file_name error:&error];if(dict!=nil){ NSLog(@"filesize = %llu ",[dict fileSize]); NSLog(@"fileModificationDate = %@",[dict fileModificationDate]); NSLog(@"fileType = %@ ",[dict fileType]); NSLog(@"filePosixPermissions = %o ",[dict filePosixPermissions]); NSLog(@"fileOwnerAccountName = %@ ",[dict fileOwnerAccountName]); NSLog(@"fileGroupOwnerAccountName = %@ ",[dict fileGroupOwnerAccountName]); NSLog(@"fileSystemNumber = %u ",[dict fileSystemNumber]); NSLog(@"fileSystemFileNumber = %u ",[dict fileSystemFileNumber]); NSLog(@"fileExtensionHidden = %d ",[dict fileExtensionHidden]); NSLog(@"fileHFSCreatorCode = %lu ",[dict fileHFSCreatorCode]); NSLog(@"fileHFSTypeCode = %lu ",[dict fileHFSTypeCode]); NSLog(@"fileIsImmutable = %d ",[dict fileIsImmutable]); NSLog(@"fileIsAppendOnly = %d ",[dict fileIsAppendOnly]); NSLog(@"fileCreationDate = %@ ",[dict fileCreationDate]); NSLog(@"fileOwnerAccountID = %@ ",[dict fileOwnerAccountID]); NSLog(@"fileGroupOwnerAccountID = %@ ",[dict fileGroupOwnerAccountID]);}使用這些添加的方法可以輸出文件的屬性。
通過NSFileManager的fileExistsAtPath方法可以判斷文件是否存在,若文件存在還可以判斷文件是否是目錄文件。 參數path指定文件路徑。參數isDirectory判斷存在文件是否是目錄。
通過NSFileManager的isReadableFileAtPath方法可以判斷文件是否可讀。 通過NSFileManager的isWritableFileAtPath方法可以判斷文件是否可寫。 通過NSFileManager的isExecutableFileAtPath方法可以判斷文件是否可執行。 通過NSFileManager的isDeletableFileAtPath方法可以判斷文件是否可刪除。
參數path指定文件路徑。
通過NSFileManager的setAttributes方法可以修改文件的屬性。 參數attributes保存修改的屬性,它是NSDictionary< NSFileAttributeKey, id>類型數據;參數path指定文件路徑;參數error在修改出錯時,顯示出錯信息。
在所有屬性中有些屬性可以修改,例如filePosixPermissions屬性。有些則不可以,例如fileType屬性。需要修改某些屬性時,就使用屬性構建NSDictionary< NSFileAttributeKey, id>對象,然后調用setAttributes方法修改屬性。
例如:
NSString *dir=NSHomeDirectory(); NSString *file_name=[dir stringByAppendingPathComponent:@"aaa"]; NSFileManager *manager=[NSFileManager defaultManager]; NSError *error=nil; NSDictionary *attr=[NSDictionary dictionaryWithObjectsAndKeys:[NSDate dateWithTimeIntervalSince1970:1000],NSFileModificationDate,[NSNumber numberWithInteger:0711],NSFilePosixPermissions,nil]; BOOL isset=[manager setAttributes:attr ofItemAtPath:file_name error:&error]; NSLog(@"------isset = %d ",isset);代碼修改了文件的NSFileModificationDate和NSFilePosixPermissions屬性。
經過測試,所有屬性中NSFileModificationDate,NSFilePosixPermissions,NSFileExtensionHidden,NSFileImmutable,NSFileAppendOnly,NSFileCreationDate屬性可以修改。
新聞熱點
疑難解答