主要做用:此类主要是对文件进行的操做(建立/删除/更名等)以及文件信息的获取。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);