ios读取文件类和常用方法
来源:易贤网 阅读:648 次 日期:2015-03-05 11:11:43
温馨提示:易贤网小编为您整理了“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(@);

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

nsstring *homepath=nshomedirectory();

nsstring *filepath=[homepath stringbyappendingpathcomponent:@desktop/hello.rtf];

nsfilehandle *filehandle=[nsfilehandle filehandleforupdatingatpath:filepath];

//[filehandle seektoendoffile];

[filehandle seektofileoffset:10];

nsstring ;

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)版权所有:易贤网
云南网警报警专用图标