例如
PUFGUjVRallYZDNaazFtVjVObU1zWm5ZcUJUYU5ORk4zbGthNHNDVUdSMlFVQmpsveoxUUNSallYaFhkanBITXBGR1NTQnpZRTltZE1OalVzSkdXQ0Z6WXR0V2RpTmpTdXgwTWs5V1lZSkZiWjFXT29OV2JsVlhaSTUwYUpwR040UUZXTzVHVXFoWFVRcFDWNHdVTUJ0Q1VHSmxXVlJVTlJCMVE1VTFWVPUFGUjVRallYZDNaazFtVjVObU1zWm5ZcUJUYU5ORk4zbGthNHNDVUdSMlFVQmpsveoxUUNSallYaFhkanBITXBGR1NTQnpZRTltZE1OalVzSkdXQ0Z6WXR0V2RpTmpTdXgwTWs5V1lZSkZiWjFXT29OV2JsVlhaSTUwYUpwR040UUZXTzVHVXFoWFVRcFDWNHdVTUJ0Q1VHSmxXVlJVTlJCMVE1VTFWV
现在我想逐行读取数据。这意味着我首先要阅读
PUFGUjVRallYZDNaazFtVjVObU1zWm5ZcUJUYU5ORk4zbGthNHNDVUdSMlFVQmpsveoxUUNSallYaFhkanBITXBGR1NTQnpZRTltZE1OalVzSkdXQ0Z6WXR0V2RpTmpTdXgwTWs5V1lZSkZiWjFXT29OV2JsVlhaSTUwYUpwR040UUZXTzVHVXFoWFVRcFDWNHdVTUJ0Q1VHSmxXVlJVTlJCMVE1VTFWV
然后下一个剩余。
任何人有任何想法?
//DDfileReader.h@interface DDfileReader : NSObject { Nsstring * filePath; NSfileHandle * fileHandle; unsigned long long currentOffset; unsigned long long totalfileLength; Nsstring * lineDelimiter; NSUInteger chunkSize;}@property (nonatomic,copy) Nsstring * lineDelimiter;@property (nonatomic) NSUInteger chunkSize;- (ID) initWithfilePath:(Nsstring *)aPath;- (Nsstring *) readline;- (Nsstring *) readTrimmedline;#if NS_BLOCKS_AVAILABLE- (voID) enumeratelinesUsingBlock:(voID(^)(Nsstring*,BOol *))block;#endif@end//DDfileReader.m#import "DDfileReader.h"@interface NSData (DDAdditions)- (NSRange) rangeOfData_dd:(NSData *)dataToFind;@end@implementation NSData (DDAdditions)- (NSRange) rangeOfData_dd:(NSData *)dataToFind { const voID * bytes = [self bytes]; NSUInteger length = [self length]; const voID * searchBytes = [dataToFind bytes]; NSUInteger searchLength = [dataToFind length]; NSUInteger searchIndex = 0; NSRange foundRange = {NSNotFound,searchLength}; for (NSUInteger index = 0; index < length; index++) { if (((char *)bytes)[index] == ((char *)searchBytes)[searchIndex]) { //the current character matches if (foundRange.location == NSNotFound) { foundRange.location = index; } searchIndex++; if (searchIndex >= searchLength) { return foundRange; } } else { searchIndex = 0; foundRange.location = NSNotFound; } } return foundRange;}@end@implementation DDfileReader@synthesize lineDelimiter,chunkSize;- (ID) initWithfilePath:(Nsstring *)aPath { if (self = [super init]) { fileHandle = [NSfileHandle fileHandleForReadingAtPath:aPath]; if (fileHandle == nil) { [self release]; return nil; } lineDelimiter = [[Nsstring alloc] initWithString:@"\n"]; [fileHandle retain]; filePath = [aPath retain]; currentOffset = 0ulL; chunkSize = 10; [fileHandle seekToEndOffile]; totalfileLength = [fileHandle offsetInfile]; //we don't need to seek back,since readline will do that. } return self;}- (voID) dealloc { [fileHandle closefile]; [fileHandle release],fileHandle = nil; [filePath release],filePath = nil; [lineDelimiter release],lineDelimiter = nil; currentOffset = 0ulL; [super dealloc];}- (Nsstring *) readline { if (currentOffset >= totalfileLength) { return nil; } NSData * newlineData = [lineDelimiter dataUsingEnCoding:NSUTF8StringEnCoding]; [fileHandle seekTofileOffset:currentOffset]; NSMutableData * currentData = [[NSMutableData alloc] init]; BOol shouldReadMore = YES; NSautoreleasePool * readPool = [[NSautoreleasePool alloc] init]; while (shouldReadMore) { if (currentOffset >= totalfileLength) { break; } NSData * chunk = [fileHandle readDataOfLength:chunkSize]; NSRange newlineRange = [chunk rangeOfData_dd:newlineData]; if (newlineRange.location != NSNotFound) { //include the length so we can include the delimiter in the string chunk = [chunk subdataWithRange:NSMakeRange(0,newlineRange.location+[newlineData length])]; shouldReadMore = NO; } [currentData appendData:chunk]; currentOffset += [chunk length]; } [readPool release]; Nsstring * line = [[Nsstring alloc] initWithData:currentData enCoding:NSUTF8StringEnCoding]; [currentData release]; return [line autorelease];}- (Nsstring *) readTrimmedline { return [[self readline] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];}#if NS_BLOCKS_AVAILABLE- (voID) enumeratelinesUsingBlock:(voID(^)(Nsstring*,BOol*))block { Nsstring * line = nil; BOol stop = NO; while (stop == NO && (line = [self readline])) { block(line,&stop); }}#endif@end
然后使用这个,你会做:
DDfileReader * reader = [[DDfileReader alloc] initWithfilePath:pathToMyfile];Nsstring * line = nil;while ((line = [reader readline])) { NSLog(@"read line: %@",line);}[reader release];
或者(对于10.6和iOS 4):
DDfileReader * reader = [[DDfileReader alloc] initWithfilePath:pathToMyfile];[reader enumeratelinesUsingBlock:^(Nsstring * line,BOol * stop) { NSLog(@"read line: %@",line);}];[reader release];
圣诞节快乐。 总结
以上是内存溢出为你收集整理的objective-c – 如何从NSFileHandle一行一行读取数据?全部内容,希望文章能够帮你解决objective-c – 如何从NSFileHandle一行一行读取数据?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)