主要做用:此類主要是對文件進行的操做(建立/刪除/更名等)以及文件信息的獲取。app
功能方法:ide
建立所需的方法在頭文件的聲明:this
/* createDirectoryAtPath:withIntermediateDirectories:attributes:error: creates a directory at the specified path. If you pass 'NO' for createIntermediates, the directory must not exist at the time this call is made. Passing 'YES' for 'createIntermediates' will create any necessary intermediate directories. This method returns YES if all directories specified in 'path' were created and attributes were set. Directories are created with attributes specified by the dictionary passed to 'attributes'. If no dictionary is supplied, directories are created according to the umask of the process. This method returns NO if a failure occurs at any stage of the operation. If an error parameter was provided, a presentable NSError will be returned by reference. This method replaces createDirectoryAtPath:attributes: */
// 參數1:建立的文件夾的路徑
// 參數2:是否建立媒介的布爾值,通常爲YES
// 參數3: 屬性,沒有就置爲nil
// 參數4: 錯誤信息 - (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(nullable NSDictionary<NSString *, id> *)attributes error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
實例代碼:編碼
// 建立對象 NSFileManager *manager = [NSFileManager defaultManager]; // 建立路徑 NSString *path = NSHomeDirectory(); path = [path stringByAppendingPathComponent:@"test/myApp"]; NSLog(@"%@", path); NSError *error = nil; // 建立文件夾 BOOL success = [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]; NSLog(@"success = %d,error = %@", success,error);
內容寫入方法在頭文件的聲明:atom
// 參數1:要寫入內容的文件的文件路徑 // 參數2:一個BOOL值,通常爲YES // 參數3: 編碼方式,通常爲UTF8 // 參數4:錯誤信息 - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;
實例代碼:spa
//向文件夾中添加字符串 path = [path stringByAppendingPathComponent:@"zifucuan.txt"]; //初始化一個字符串 NSString *string = @"hello"; BOOL success1 = [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; if (success1) { NSLog(@"成功:%@",path); }else{ NSLog(@"失敗"); }
刪除文件方法在頭文件的聲明:code
// 參數1:路徑 // 參數2:錯誤信息 - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
實例代碼:對象
// 刪除path目錄下的全部文件 [manager removeItemAtPath:path error:nil];
文件移動方法在頭文件的聲明:blog
// 參數1:要移動的文件路徑 // 參數2:要移動到的文件路徑(目的地) // 參數3:錯誤信息 - (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
實例代碼:ci
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 建立一個文件夾 NSString *copyPath = [documentPath stringByAppendingPathComponent:@"備份/test.txt"]; // stringByDeletingLastPathComponent 刪除最後一個路徑 [manager createDirectoryAtPath:[copyPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil]; // 定義一個字符串 NSString *testStr = @"Hello World"; NSData *data = [testStr dataUsingEncoding:NSUTF8StringEncoding]; // 將內容寫入文件 [manager createFileAtPath:copyPath contents:data attributes:nil]; // 建立一個toPath NSString *toPath = [documentPath stringByAppendingPathComponent:@"hello/copyTest.txt"]; // 建立一個移動到的文件夾及文件 [manager createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil]; BOOL result = [manager moveItemAtPath:copyPath toPath:toPath error:nil]; NSLog(@"result = %d", result);
文件copy(拷貝)方法在頭文件的聲明:
// 參數1:要拷貝的文件路徑
// 參數2:要拷貝到的文件路徑(目的地)
// 參數3:錯誤信息
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
實例代碼:
// 路徑使用上面的路徑 [manager copyItemAtPath:copyPath toPath:toPath error:nil];
NSFileHandle 是很是基礎的只針對文件內容的操做(寫入,讀取,更新),是把NSData,經過鏈接器一個字節一個字節的寫入/讀取文件.(NSData <—> NSFileHandle <—> 文件).
使用場景: 對文件內容的進行局部修改、追加內容。
使用步驟
1).文件對接並獲取一個NSFileHandle對象.
2).讀寫操做
3).關閉對接
注意:NSFileHandle 類並沒有提供建立文件的功能。必須使用 NSFileManager 方法來建立文件。所以,在使用下圖表中的方法時,都是保證文件已經存在,不然返回nil.
// 參數爲文件路徑 + (nullable instancetype)fileHandleForUpdatingAtPath:(NSString *)path;
// 搜索到文件內容的末尾 - (unsigned long long)seekToEndOfFile;
// 建立handle對象 NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path]; // 搜索到文本內容末尾 [fileHandle seekToEndOfFile]; NSString *appendStr = @"我是後來的"; NSData *appendData = [appendStr dataUsingEncoding:NSUTF8StringEncoding]; // 將數據寫入到對接起 [fileHandle writeData:appendData]; // 關閉對接起 [fileHandle closeFile];
4> 定位數據
// 參數爲文件路徑
+ (nullable instancetype)fileHandleForReadingAtPath:(NSString *)path;
@property (readonly, copy) NSData *availableData;
// 參數爲一個和文件長度有關的數值 - (void)seekToFileOffset:(unsigned long long)offset;
- (NSData *)readDataToEndOfFile;
實例代碼:
// 將「123456」寫入file2.txt文件夾中 NSString * content = @"123456"; NSString * filePath2 = [documentPath stringByAppendingPathComponent:@"file2.txt"]; [fileManager createFileAtPath:filePath2 contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]; // 經過fileHandle讀取 fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath2]; // 獲取數據長度 NSUInteger length = [[fileHandle availableData] length]; // 設置文件的偏移量爲文件的一半 [fileHandle seekToFileOffset:length/2.0]; // 從文件的偏移量位置讀取到最後 NSData * data = [fileHandle readDataToEndOfFile]; [fileHandle closeFile]; // 打印讀取的字符串 NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",string);