文件管理類NSFileManager常用操作
來源:程序員人生 發布時間:2015-04-14 08:50:15 閱讀次數:3017次
1、創建1個文件管理器
NSFileManager *fm = [NSFileManager defaultManager];
2、淺度遍歷目錄
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
3、深度遍歷目錄
- (NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
4、獲得當前目錄
- (NSString *)currentDirectoryPath
5、更改當前目錄
- (BOOL)changeCurrentDirectoryPath:(NSString *)path
6、枚舉目錄內容
- (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path
7、創建目錄
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error
8、創建文件
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)contents attributes:(NSDictionary *)attributes
9、復制文件
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
10、刪除文件
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
11、目錄/文件拷貝
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
12、移動/重命名文件或目錄
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
13、測試文件是不是存在
- (BOOL)fileExistsAtPath:(NSString *)path
14、獲得文件信息(屬性和權限)
- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error
15、從文件中讀取數據
- (NSData *)contentsAtPath:(NSString *)path
16、比較兩個文件的內容
- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2
17、測試文件是不是存在,且是不是能履行讀操作
- (BOOL)isReadableFileAtPath:(NSString *)path
18、測試文件是不是存在,且是不是能履行寫操作
- (BOOL)isWritableFileAtPath:(NSString *)path
2、文件操作類NSFileHandle經常使用操作:
1、只讀方式打開文件
+ (id)fileHandleForReadingAtPath:(NSString *)path
2、只寫方式打開文件
+ (id)fileHandleForWritingAtPath:(NSString *)path
3、讀寫方式打開文件
+ (id)fileHandleForUpdatingAtPath:(NSString *)path
4、從文件當前位置讀到結尾
- (NSData *)readDataToEndOfFile
5、從文件當前位置讀固定字節數的內容
- (NSData *)readDataOfLength:(NSUInteger)length
6、返回所有可用的數據
- (NSData *)availableData
7、寫文件
- (void)writeData:(NSData *)data
8、定位到文件尾部
- (unsigned long long)seekToEndOfFile
9、定位到文件指定位置
- (void)seekToFileOffset:(unsigned long long)offset
10、獲得當前文件的偏移量
- (unsigned long long)offsetInFile
11、將文件的長度設置為offset字節
- (void)truncateFileAtOffset:(unsigned long long)offset
關閉文件
- (void)closeFile
P.S. (網絡socket中)通過initWithFileDescriptor初始化的對象,需要顯式調用此方法;其它方法創建的對象會自動打開文件,該對象被燒毀時會自動關閉該方法,不需顯式調用此方法。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈