iOS沙盒 -- 文件对接器(NSFileHandle)
来源:互联网 发布:淘宝使用他人图片扣分 编辑:程序博客网 时间:2024/06/03 11:32
文件对接器(NSFileHandle)
侧重于对文内容的管理,我们可以使用它完成对文件的继续写入,部分读取
在使用文件对接器之前我们必须保证文件路径上,是存在文件的
拼接想创建文件的路径
NSString*cachePath =[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)firstObject];
NSString*newFilePath = [cachePathstringByAppendingPathComponent:@"hanleFile.txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:newFilePath] == NO) {
NSString *str = @"class49 is so pretty";
BOOL result = [fileManager createFileAtPath:newFilePathcontents:[str dataUsingEncoding:NSUTF8StringEncoding]attributes:nil];
if (result) {
向文件中继续写入数据
NSFileHandle *writeHandle = [NSFileHandlefileHandleForReadingAtPath:newFilePath];
移动光标节点,挪到最后
[writeHandle seekToEndOfFile];
向文件中继续写入内容
NSString *writeStr = @", very great";
NSData *writeData = [writeStrdataUsingEncoding:NSUTF8StringEncoding];
[writeHandle writeData:writeData];
关闭对接器对应的文件
[writeHandle closeFile];
}
}
文件部分的读取
NSFileHandle*readHandle = [NSFileHandlefileHandleForReadingAtPath:newFilePath];
读取字符串长度为1的字符串
NSData*readData = [readHandle readDataOfLength:1];
读取后面一半的长度
读取中数据的长度
NSIntegerlenght = [readHandle availableData].length;
移动光标节点,挪到一半的距离
[readHandleseekToFileOffset:lenght / 2];
当我们想要获取全部内容,也需要手动设置seekToFileOffset,设置为0
读取数据
NSData*readData = [readHandle readDataToEndOfFile];
NSString*readStr = [[NSString alloc]initWithData:readDataencoding:NSUTF8StringEncoding];
关闭文件
[readHandlecloseFile];
NSLog(@"readStr = %@",readStr);
侧重于对文内容的管理,我们可以使用它完成对文件的继续写入,部分读取
在使用文件对接器之前我们必须保证文件路径上,是存在文件的
拼接想创建文件的路径
文件部分的读取
读取字符串长度为1的字符串
读取后面一半的长度
读取中数据的长度
移动光标节点,挪到一半的距离
当我们想要获取全部内容,也需要手动设置seekToFileOffset,设置为0
读取数据
关闭文件
0
喜欢
0 0
- iOS沙盒 -- 文件对接器(NSFileHandle)
- ios文件存储--NSFileHandle
- iOS文件管理 NSFileManager NSFileHandle
- iOS文件系统---沙盒(sandbox),NSFileManager,NSFileHandle, NSData, 目录,文件
- iOS文件操作NSFileManager和NSFileHandle
- iOS文件流操作类-NSFileHandle
- ios 之文件操作NSFileMange和NSFileHandle
- 【ios 汇总 - 操作文件常用方法】 (NSFileManager NSFileHandle NSData)
- 【iOS】第03讲 文件读写/NSArray/NSData/NSFileManager/NSFileHandle
- iOS NSFileHandle使用
- iOS基础一 NSFileHandle
- IOS NSFileHandle 基本用法
- iOS NSFileHandle详解
- ios开发之NSFileHandle
- iOS NSFileHandle的使用
- ios-NSFileHandle和NSOutputStream
- NSFileHandle操作文件内容
- NSFileHandle处理文件
- 1032. 挖掘机技术哪家强(20)
- Android-Android studio加载9.png图片报错
- Android开发优化之——对界面UI的优化(1)
- Hibernate4一对多关系映射
- jquery-number
- iOS沙盒 -- 文件对接器(NSFileHandle)
- 4——new/delete 与 malloc/free 的区别是什么?(我的搜集,持续中。。。。)
- 欢迎使用CSDN-markdown编辑器
- 单起一个事物
- Studio项目提交到github步骤
- 我们不仅仅分享技术,我们还教你如何健身!
- 【c++】念数字
- cordova安装
- CSS段落首行缩进