大纲
NSFileHandle 此类主要是对文件内容进行读取和写入操作
NSFileManager 此类主要是对文件进行的操作以及文件信息的获取
##常用处理方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| /*从设备或通道返回可用的数据*/ @property (readonly, copy) NSData *availableData; //打开一个文件准备读取 + (nullable instancetype)fileHandleForReadingAtPath:(NSString *)path; //打开一个文件准备写入 + (nullable instancetype)fileHandleForWritingAtPath:(NSString *)path; //打开一个文件准备更新 + (nullable instancetype)fileHandleForUpdatingAtPath:(NSString *)path; //从当前的节点读取到文件的末尾 - (nullable NSData *)readDataToEndOfFileAndReturnError:(out NSError **)error; //从当前节点开始读取指定的长度数据 - (nullable NSData *)readDataUpToLength:(NSUInteger)length error:(out NSError **)error; //写入数据 - (BOOL)writeData:(NSData *)data error:(out NSError **)error; //获取当前文件的偏移量 - (BOOL)getOffset:(out unsigned long long *)offsetInFile error:(out NSError **)error; //跳到指定文件的偏移量 - (BOOL)seekToOffset:(unsigned long long)offset error:(out NSError **)error; //跳到文件末尾 - (BOOL)seekToEndReturningOffset:(out unsigned long long *_Nullable)offsetInFile error:(out NSError **)error; //将文件的长度设为offset字节 - (BOOL)truncateAtOffset:(unsigned long long)offset error:(out NSError **)error; //关闭文件 - (BOOL)closeAndReturnError:(out NSError **)error;
|
##向文件追加数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| NSString *homePath = NSHomeDirectory( );
NSString *sourcePath = [homePath stringByAppendingPathConmpone:@"testfile.text"];
NSFileHandle *fielHandle = [NSFileHandle fileHandleForUpdatingAtPath:sourcePath];
[fileHandle seekToEndOfFile]; 将节点跳到文件的末尾
NSString *str = @"追加的数据"
NSData* stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
[fileHandle writeData:stringData]; 追加写入数据
[fileHandle closeFile];
|
##定位数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| NSFileManager *fm = [NSFileManager defaultManager];
NSString *content = @"abcdef";
[fm createFileAtPath:path contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSUInteger length = [fileHandle availabelData] length]; 获取数据长度
[fileHandle seekToFileOffset;length/2]; 偏移量文件的一半
NSData *data = [fileHandle readDataToEndOfFile];
[fileHandle closeFile];
|
##复制文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| // 输入文件、输出文件 NSFileHandle *infile, *outfile;NSData *buffer; //读取的缓冲数据 NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *homePath = NSHomeDirectory( ); // 源文件路径 NSString *sourcePath = [homePath stringByAppendingPathComponent:@"testfile.txt"]; //输出文件路径 NSString *outPath = [homePath stringByAppendingPathComponent:@"outfile.txt"]; BOOL sucess = [fileManager createFileAtPath:outPath contents:nil attributes:nil]; if (!success){ return N0; } //创建读取源路径文件 infile = [NSFileHandle fileHandleForReadingAtPath:sourcePath]; if (infile == nil){ return NO; } //创建病打开要输出的文件 outfile = [NSFileHandle fileHandleForReadingAtPath:outPath]; if (outfile == nil){ return NO; } //将输出文件的长度设为0 [outfile truncateFileAtOffset:0]; //读取数据 buffer = [infile readDataToEndOfFile]; // 写入输入 [outfile writeData:buffer]; //关闭写入、输入文件 [infile closeFile]; [outfile closeFile];
|