标题 | 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 ; 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]; |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。