您的位置:首页 > 移动开发 > IOS开发

ios开发之获取修改文件属性

2017-02-06 19:21 495 查看
ios提供了一些API用于获取和修改文件属性。本文将详细讨论ios中如何获取与修改文件属性。

每个文件都有相应的属性。这些属性可能包含文件大小,文件修改日期,文件权限…我们可以获取这些属性,也可以修改某些属性。

文件的基本结构



获取文件属性

NSFileManager
- (nullable NSDictionary<NSFileAttributeKey, id> *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error


通过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
- (BOOL)fileExistsAtPath:(NSString *)path;
- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(nullable BOOL *)isDirectory;


通过NSFileManager的fileExistsAtPath方法可以判断文件是否存在,若文件存在还可以判断文件是否是目录文件。

参数path指定文件路径。参数isDirectory判断存在文件是否是目录。

NSFileManager
- (BOOL)isReadableFileAtPath:(NSString *)path;
- (BOOL)isWritableFileAtPath:(NSString *)path;
- (BOOL)isExecutableFileAtPath:(NSString *)path;
- (BOOL)isDeletableFileAtPath:(NSString *)path;```


通过NSFileManager的isReadableFileAtPath方法可以判断文件是否可读。

通过NSFileManager的isWritableFileAtPath方法可以判断文件是否可写。

通过NSFileManager的isExecutableFileAtPath方法可以判断文件是否可执行。

通过NSFileManager的isDeletableFileAtPath方法可以判断文件是否可删除。

参数path指定文件路径。

修改文件属性

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


通过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属性可以修改。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios api ios开发