iOS 文件屬性讀取與寫入

2、文件屬性的讀取與修改

2.1文件屬性讀取
    NSString *strPath =[[NSBundle mainBundle] pathForResource:@"lomo.jpg" ofType:nil];  
    NSLog(@"path:%@", strPath);  
    NSFileManager *fileManager = [NSFileManager defaultManager];  
    NSString *path = strPath;//@"/tmp/List";  
    NSError *error = nil;  
    NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:&error];  
      
    if (fileAttributes != nil) {  
        NSNumber *fileSize = [fileAttributes objectForKey:NSFileSize];  
        NSString *fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName];  
        NSDate *fileModDate = [fileAttributes objectForKey:NSFileModificationDate];  
        NSDate *fileCreateDate = [fileAttributes objectForKey:NSFileCreationDate];  
        if (fileSize) {  
            NSLog(@"File size: %qi\n", [fileSize unsignedLongLongValue]);  
        }  
        if (fileOwner) {  
            NSLog(@"Owner: %@\n", fileOwner);  
        }  
        if (fileModDate) {  
            NSLog(@"Modification date: %@\n", fileModDate);  
        }  
        if (fileCreateDate) {  
            NSLog(@"create date:%@\n", fileModDate);  
        }  
    }  
    else {  
        NSLog(@"Path (%@) is invalid.", path);  
    }
2.2文件屬性修改與寫入

咱們從2.1可知,文件屬性具備相似,而不是簡單隻有的NSString或者NSInteger類型,所以,咱們的寫入類型和讀取類型一致即可以寫入。html

- (BOOL)setAttributes:(NSDictionary<NSString *, id> *)attributes ofItemAtPath:(NSString *)path error:(NSError * _Nullable *)error

 主要屬性以下java

NSFileBusy,ios

NSFileCreationDate,緩存

NSFileExtensionHidden,app

NSFileGroupOwnerAccountID,測試

NSFileGroupOwnerAccountName,spa

NSFileHFSCreatorCode,code

NSFileHFSTypeCode,orm

NSFileImmutable,htm

 NSFileModificationDate,

NSFileOwnerAccountID,

NSFileOwnerAccountName

NSFilePosixPermissions.



NSError &error;
[NSFileManager defaultManager] setAttributes:@{NSFileModificationDate:[NSDate date],NSFileCreationDate:[NSDate date],NSFileOwnerAccountName:@""}  ofItemAtPath:filePath error:&error];

NSFileOwnerAccountName咱們能夠在設置成http中的eTag


必要性:修改文件modify時間能夠讓咱們移動開發中更好的處理斷點續傳和http304緩存。


注意:

有些文件能夠修改爲功,有些則修改失敗。

目前測試不足,具體能夠肯定,非執行文件的修改時間能夠修改爲功,相似html可執行文件則出現了失敗狀況。

相關文章
相關標籤/搜索