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.1可知,文件属性具备相似,而不是简单只有的NSString或者NSInteger类型,所以,咱们的写入类型和读取类型一致即可以写入。html
- (BOOL)setAttributes:(NSDictionary<NSString *, id> *)attributes ofItemAtPath:(NSString *)path error:(NSError * _Nullable *)error
主要属性以下java
NSFileBusy
,ios
NSFileGroupOwnerAccountName
,spa
NSFileHFSCreatorCode
,code
NSFileImmutable
,htm
NSError &error; [NSFileManager defaultManager] setAttributes:@{NSFileModificationDate:[NSDate date],NSFileCreationDate:[NSDate date],NSFileOwnerAccountName:@""} ofItemAtPath:filePath error:&error];
NSFileOwnerAccountName咱们能够在设置成http中的eTag
必要性:修改文件modify时间能够让咱们移动开发中更好的处理断点续传和http304缓存。
注意:
有些文件能够修改为功,有些则修改失败。
目前测试不足,具体能够肯定,非执行文件的修改时间能够修改为功,相似html可执行文件则出现了失败状况。