码迷,mamicode.com
首页 > 其他好文 > 详细

NSFileHandle

时间:2015-11-20 16:50:07      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:

 

    

    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

标签:

原文地址:http://www.cnblogs.com/xiangjune/p/4981018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!