ios开发之获取修改文件属性
2017-02-06 19:21
495 查看
ios提供了一些API用于获取和修改文件属性。本文将详细讨论ios中如何获取与修改文件属性。
每个文件都有相应的属性。这些属性可能包含文件大小,文件修改日期,文件权限…我们可以获取这些属性,也可以修改某些属性。
文件的基本结构
通过NSFileManager的attributesOfItemAtPath方法可以获取文件的属性。
参数path指定文件路径,参数error在获取出错时,显示出错信息。
若获取失败则返回nil,出错信息保存在error中。若获取成功则返回NSDictionary< NSFileAttributeKey, id>对象。文件的所有属性都保存在NSDictionary< NSFileAttributeKey, id>对象中。
ios为NSDictionary< NSFileAttributeKey, id>添加了一些方法用于获取属性值。
例如:
使用这些添加的方法可以输出文件的属性。
通过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方法修改属性。
例如:
代码修改了文件的NSFileModificationDate和NSFilePosixPermissions属性。
经过测试,所有属性中NSFileModificationDate,NSFilePosixPermissions,NSFileExtensionHidden,NSFileImmutable,NSFileAppendOnly,NSFileCreationDate属性可以修改。
每个文件都有相应的属性。这些属性可能包含文件大小,文件修改日期,文件权限…我们可以获取这些属性,也可以修改某些属性。
文件的基本结构
获取文件属性
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属性可以修改。
相关文章推荐
- iphone ios 文件系统操作(创建、删除,修改,获取属性)
- IOS开发——获取本地音频文件(属性/信息)
- IOS开发——获取本地音频文件(属性/信息)
- IOS开发——获取本地音频文件(属性/信息)
- iOS开发-获取各种文件的目录路径的方法
- IOS获取plist文件属性值的方法
- ios 文件管理 获取文件属性 读取文件
- 文件时间属性获取与修改
- 【iOS开发-40】不能直接修改对象的结构体属性的成员,那怎么办?
- MFC中获取文件的属性(包括文件创建时间、修改时间、访问时间、文件大小等)
- ios 文件管理 获取文件属性 读取文件
- delphi获取文件或文件夹属性和修改属性
- [转+修改]iOS开发中会经常用到文件上传下载类
- iOS开发备忘录:属性列表文件数据持久化
- adb shell获取root权限 因为开发需要,我经常会用到adb这个工具(Android Debug Bridge),我们都知道adb shell默认是没有root权限的,修改系统文件就很不方便
- VC++ 获取文件属性创建时间、修改时间和访问时间
- 怎么获取文件的“修改时间”属性。
- iPhone SDK开发 文件权限获取及修改
- AS/400开发经验点滴(二)一个批量修改文件属性的工具
- 【iOS开发-3】sandbox沙盒介绍以及如何取得沙盒里面各文件的路径,图片导入方式和图片路径获取