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