objective-c – 如何从NSFileHandle一行一行读取数据?

objective-c – 如何从NSFileHandle一行一行读取数据?,第1张

概述我有一个文本文件具有给定的数据 例如 PUFGUjVRallYZDNaazFtVjVObU1zWm5ZcUJUYU5ORk4zbGthNHNDVUdSMlFVQmpSVEoxUUNSallYaFhkanBITXBGR1NTQnpZRTltZE1OalVzSkdXQ0Z6WXR0V2RpTmpTdXgwTWs5V1lZSkZiWjFXT29OV2JSVlhaSTUwYUpwR040UUZXTzVHV 我有一个文本文件具有给定的数据

例如

PUFGUjVRallYZDNaazFtVjVObU1zWm5ZcUJUYU5ORk4zbGthNHNDVUdSMlFVQmpsveoxUUNSallYaFhkanBITXBGR1NTQnpZRTltZE1OalVzSkdXQ0Z6WXR0V2RpTmpTdXgwTWs5V1lZSkZiWjFXT29OV2JsVlhaSTUwYUpwR040UUZXTzVHVXFoWFVRcFDWNHdVTUJ0Q1VHSmxXVlJVTlJCMVE1VTFWVPUFGUjVRallYZDNaazFtVjVObU1zWm5ZcUJUYU5ORk4zbGthNHNDVUdSMlFVQmpsveoxUUNSallYaFhkanBITXBGR1NTQnpZRTltZE1OalVzSkdXQ0Z6WXR0V2RpTmpTdXgwTWs5V1lZSkZiWjFXT29OV2JsVlhaSTUwYUpwR040UUZXTzVHVXFoWFVRcFDWNHdVTUJ0Q1VHSmxXVlJVTlJCMVE1VTFWV

现在我想逐行读取数据。这意味着我首先要阅读

PUFGUjVRallYZDNaazFtVjVObU1zWm5ZcUJUYU5ORk4zbGthNHNDVUdSMlFVQmpsveoxUUNSallYaFhkanBITXBGR1NTQnpZRTltZE1OalVzSkdXQ0Z6WXR0V2RpTmpTdXgwTWs5V1lZSkZiWjFXT29OV2JsVlhaSTUwYUpwR040UUZXTzVHVXFoWFVRcFDWNHdVTUJ0Q1VHSmxXVlJVTlJCMVE1VTFWV

然后下一个剩余。
任何人有任何想法?

解决方法 如果你的文件很小,那么@ mipadi的方法可能会很好。但是,如果您的文件很大(> 1MB,也许?),那么您可能需要考虑逐行读取文件。我写了一个类来做这个,我会粘在这里:

//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一行一行读取数据?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1046242.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存