手机
当前位置:查字典教程网 >编程开发 >IOS开发 >IOS读取文件类和常用方法
IOS读取文件类和常用方法
摘要:第一、NSFileHandleNSFileManager类主要对文件的操作(删除、修改、移动、复制等)NSFileHandle类主要对文件的...

第一、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];

【IOS读取文件类和常用方法】相关文章:

IOS设置QQ小红点消除的方法(一键退朝)

iOS开发中使用SQL语句操作数据库的基本用法指南

iOS应用开发中的文字选中操作控件UITextView用法讲解

iOS购物分类模块的实现方案

ios的文件加载和保存

iOS应用中UILabel文字显示效果的常用设置总结

详解iOS App中UiTabBarController组件的基本用法

iOS实现选项卡效果的方法

详解IOS中如何实现瀑布流效果

iOS开发中常见的项目文件与MVC结构优化思路解析

精品推荐
分类导航