NSFileHandle

 

    

    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentPath = [pathArray firstObject];

    

    NSString *filePath = [documentPath stringByAppendingPathComponent:@"test.txt"];

    

    // 读取文件 1.创建 fileHandle 2.读写操作 3.关闭文件

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];

    NSData *data = [fileHandle readDataToEndOfFile];

    NSString *content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    

    [fileHandle closeFile];

    

    // 写入文件

    NSFileHandle *writeFileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

    content = [content stringByAppendingString:@" new writing..."];

    NSData *writeData = [content dataUsingEncoding:NSUTF8StringEncoding];

    [writeFileHandle writeData:writeData];

    

    [writeFileHandle closeFile];

 

你可能感兴趣的:(NSFileHandle)