Reading the bytes from a text file using a dispatch I/O channel
2014-08-28 14:42
423 查看
- (void)readContentsOfFile:(NSURL*)anURL { // Open the channel for reading. NSString* filePath = [anURL path]; self.channel = dispatch_io_create_with_path(DISPATCH_IO_RANDOM,  // Convert to C-string // Open for reading // No extra flags dispatch_release(self.channel); self.channel = nil; } [filePath UTF8String], O_RDONLY, 0, dispatch_get_main_queue(), ^(int error){ // Cleanup code if (error == 0) { }); // If the file channel could not be created, just abort. if (!self.channel) return; // Get the file size. NSNumber* theSize; NSInteger fileSize = 0; if ([anURL getResourceValue:&theSize forKey:NSURLFileSizeKey error:nil]) fileSize = [theSize integerValue]; // Break the file into 1024 size strings. size_t chunkSize = 1024; off_t currentOffset = 0; for (currentOffset = 0; currentOffset < fileSize; currentOffset += chunkSize) { dispatch_io_read(self.channel, currentOffset, chunkSize, dispatch_get_main_queue(), ^(bool done, dispatch_data_t data, int error){ if (error) return; // Build strings from the data. dispatch_data_apply(data, (dispatch_data_applier_t)^(dispatch_data_t region,  size_t size){ init]; initWithBytes:buffer size_t offset, const void *buffer, NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] NSString* aString = [[[NSString alloc] length:size encoding:NSUTF8StringEncoding] [self addString:aString toFile:anURL]; // Custom method. [pool release]; return true; // Keep processing if there is more data. }); }); autorelease]; } }
相关文章推荐
- Did you know… You can keep recently-used files from falling off the File Tab Channel?
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- Generate the java class from XML Schema file using JAXB
- The file “city.txt” couldn’t be opened using text encoding Unicode (UTF-8)
- python csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
- 《基于MFC的OpenGL编程》Part 18 Reading objects from the OBJ File Format
- KettleXMLException: 错误从XML文件读取转换 (error during reading transform from the xml file )
- Generate the java class from XML Schema file using JAXB
- C# .NET - Sql Bulk Insert from multiple delimited Textfile using c#.net
- Upload Data from Excel File in ABAP using TEXT_CONVERT_XLS_TO_SAP
- How to sovle the VSS error -- Error reading from file
- Error reading from file ... .msi Verify that the file exists and that you ...
- 使用cocapods报错 [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes
- Reading the contents of a file using NSFileHandle
- Generate the java class from XML Schema file using JAXB
- Improving performance of reading text from a large text file
- Reading the contents of a file using POSIX functions
- Error: iterator should return strings, not bytes (did you open the file in text mode?)
- fatal error C1189: #error : "This file was generated using the moc from 4.4.3. It
- KettleXMLException: 错误从XML文件读取转换 (error during reading transform from the xml file )