IOS读取文件类和常用方法
来源:易贤网 阅读:1969 次 日期:2014-11-04 09:24:15
温馨提示:易贤网小编为您整理了“IOS读取文件类和常用方法”,方便广大网友查阅!

第一、NSFileHandle

NSFileManager类主要对文件的操作(删除、修改、移动、复制等)

NSFileHandle类主要对文件的内容进行读取和写入

第二、NSFileHandle类处理文件的步骤

创建一个NSFileHandle对象

对打开的文件进行I/0操作

关闭文件

可以使用NSFileHandle进行断点续传

第三、实现查找功能的代码:

NSString *homePath=NSHomeDirectory();

NSString *filePath=[homePath stringByAppendingPathComponent:@"Desktop/hello.rtf"];

NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath:filePath];

NSUInteger length=[fileHandle availableData].length;

[fileHandle seekToFileOffset:length/2];

NSData *data=[fileHandle readDataToEndOfFile];

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

NSLog(@"%@",str);

第四、实现追加文件代码:

NSString *homePath=NSHomeDirectory();

NSString *filePath=[homePath stringByAppendingPathComponent:@"Desktop/hello.rtf"];

NSFileHandle *fileHandle=[NSFileHandle fileHandleForUpdatingAtPath:filePath];

//[fileHandle seekToEndOfFile];

[fileHandle seekToFileOffset:10];

NSString *str=@"data";

NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];

[fileHandle writeData:data];

[fileHandle closeFile];

// insert code here...

NSLog(@"Hello, World!");

第五、实现复制文件的代码:

NSString *homePath=NSHomeDirectory();

NSString *filePath=[homePath stringByAppendingPathComponent:@"Desktop/hello.rtf"];

NSString *objPath=[homePath stringByAppendingPathComponent:@"Desktop/copy.rtf"];

NSFileManager *fileManager=[NSFileManager defaultManager];

BOOL success=[fileManager createFileAtPath:objPath contents:nil attributes:nil];

NSFileHandle *writeFile=[NSFileHandle fileHandleForReadingAtPath:filePath];

NSFileHandle *objFile=[NSFileHandle fileHandleForWritingAtPath:objPath];

[objFile readDataToEndOfFile];

NSData *data=[writeFile readDataToEndOfFile];

[objFile writeData:data];

[writeFile closeFile];

[objFile closeFile];

更多信息请查看IT技术专栏

更多信息请查看技术文章
易贤网手机网站地址:IOS读取文件类和常用方法
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标