美文网首页
iOS疯狂详解之NSFileHandle

iOS疯狂详解之NSFileHandle

作者: 每天刷两次牙 | 来源:发表于2017-03-10 17:41 被阅读135次

//  创建一个文件

- (void)addField

{

NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString *fieldPath = [documentPath stringByAppendingPathComponent:@"student.txt"];

NSLog(@"%@",fieldPath);

NSString *txt = @"wanglong";

[txt writeToFile:fieldPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

//  从路径中 读取文件 获取NSFileHandle对象

NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:fieldPath];

//  获取文件内容的 结束偏移量

unsigned long long endOffset = handle.seekToEndOfFile;

[handle seekToFileOffset:4];

//  获取当前的偏移量 初始偏移量为0

unsigned long long offset = handle.offsetInFile;

//  准备要写入的字符串

NSString *str = @"123";

NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

[handle writeData:data];

NSString *saveStr = [NSString stringWithContentsOfFile:fieldPath encoding:NSUTF8StringEncoding error:nil];

NSLog(@"%@",saveStr);

//  -------------------------------------------------------

//  覆盖数据

NSFileHandle * srcFile, * destFile; //  输入文件、输出文件

NSData * buffer;//  读取的缓冲数据

NSFileManager * manager = [NSFileManager defaultManager];

NSString * sourcePath = [documentPath stringByAppendingPathComponent:@"sourceFile.txt"];//  源文件路径

NSLog(@"%@",sourcePath);

[manager createFileAtPath:sourcePath contents:[@"Hello" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

NSString * destPath = [documentPath stringByAppendingPathComponent:@"destFile.txt"];//  输出文件路径

BOOL success = [manager createFileAtPath:destPath contents:[@"abcdef" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

if (!success) {

return;

}

srcFile = [NSFileHandle fileHandleForReadingAtPath:sourcePath];

if (!destFile) {

return;

}

destFile = [NSFileHandle fileHandleForWritingAtPath:destPath];

if (!destFile) {

return;

}

[destFile truncateFileAtOffset:0];//  将输出文件的长度设为0,(也就是将原来的文件清空)

buffer = [srcFile readDataToEndOfFile];//  读取数据

[destFile writeData:buffer];

[srcFile closeFile];

[destFile closeFile];

}

相关文章

  • iOS疯狂详解之NSFileHandle

    // 创建一个文件 - (void)addField { NSString *documentPath = [NS...

  • iOS Block实例

    iOS之Block详解:Block详解 ViewController.h(ARC) ViewController....

  • 2021-08-25

    iOS swift SDK详解之NSCoding协议 详解:NSCoding是对iOS中的Model类进行编码和解...

  • iOS NSFileHandle

    https://www.kancloud.cn/digest/ftdios/122303

  • iOS NSFileHandle

    前言 众所周知,断点续传可以通过句柄 NSFileHandle 实现,那 NSFileHandle 类的主要作用是...

  • 单例

    iOS单例模式iOS之单例模式初探iOS单例详解

  • CGAffineTransform

    参考: iOS形变之CGAffineTransform iOS 仿射变换CGAffineTransform详解 如...

  • iOS开发之获取照片&&TZImagePicke

    iOS开发之获取照片&&TZImagePickerController的使用 iOS 开发之照片框架详解 objc...

  • uiscrollview

    IOS学习笔记——iOS组件之UIScrollView详解图解UIScrollView的contentOffset...

  • IOS学习(10)-UIImageView

    iOS开发笔记--UIImageView的属性之animationImages详解 IOS-UIImageView...

网友评论

      本文标题: iOS疯狂详解之NSFileHandle

      本文链接:https://www.haomeiwen.com/subject/hfrogttx.html