1、創(chuàng)建一個文件管理器
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、枚舉目錄內(nèi)容(NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path
7、創(chuàng)建目錄(BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error
8、創(chuàng)建文件
(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、獲取文件信息(屬性和權(quán)限)(NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error
15、從文件中讀取數(shù)據(jù)(NSData *)contentsAtPath:(NSString *)path
16、比較兩個文件的內(nèi)容(BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2
17、測試文件是否存在,且是否能執(zhí)行讀操作(BOOL)isReadableFileAtPath:(NSString *)path
18、測試文件是否存在,且是否能執(zhí)行寫操作(BOOL)isWritableFileAtPath:(NSString *)path
二、文件操作類NSFileHandle常用操作:
1、只讀方式打開文件
(id)fileHandleForReadingAtPath:(NSString *)path
2、只寫方式打開文件(id)fileHandleForWritingAtPath:(NSString *)path
3、讀寫方式打開文件(id)fileHandleForUpdatingAtPath:(NSString *)path
4、從文件當前位置讀到結(jié)尾
(NSData *)readDataToEndOfFile
5、從文件當前位置讀固定字節(jié)數(shù)的內(nèi)容(NSData *)readDataOfLength:(NSUInteger)length
6、返回所有可用的數(shù)據(jù)(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字節(jié)(void)truncateFileAtOffset:(unsigned long long)offset
關(guān)閉文件(void)closeFile
P.S. (網(wǎng)絡socket中)通過initWithFileDescriptor初始化的對象,需要顯式調(diào)用此方法;其它方法創(chuàng)建的對象會自動打開文件,該對象被銷毀時會自動關(guān)閉該方法,不需顯式調(diào)用此方法。