手机
当前位置:查字典教程网 >编程开发 >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中sqlite的详细用法

ios系统下删除文件的代码

ios的文件加载和保存

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

IOS设备上给body绑定click事件不生效的原因及解决办法

iOS 按钮上的文字添加下划线的方法

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

iOS开发之触摸事件以及手势

实例解析iOS应用多线程开发中NSthread类的用法

IOS关闭键盘的方法

精品推荐
分类导航