141

Objective-C で大きなテキスト ファイルを処理する適切な方法は何ですか? 各行を個別に読み取る必要があり、各行を NSString として扱いたいとしましょう。これを行う最も効率的な方法は何ですか?

1 つの解決策は、NSString メソッドを使用することです。

+ (id)stringWithContentsOfFile:(NSString *)path 
      encoding:(NSStringEncoding)enc 
      error:(NSError **)error 

次に改行区切りで行を分割し、配列内の要素を反復処理します。ただし、これはかなり効率が悪いようです。一度にすべてを読み取るのではなく、ファイルをストリームとして扱い、各行を列挙する簡単な方法はありませんか? Java の java.io.BufferedReader のようなものです。

4

18 に答える 18

93

Stringこれは、からの一般的な読み取りに機能しTextます。より長いテキスト(テキストのサイズが大きい)を読みたい場合は、バッファリング(メモリ空間にテキストのサイズを予約する)など、他の人がここで言及した方法を使用してください。

テキスト ファイルを読んだとします。

NSString* filePath = @""//file path...
NSString* fileRoot = [[NSBundle mainBundle] 
               pathForResource:filePath ofType:@"txt"];

新しい行を取り除きたい。

// read everything from text
NSString* fileContents = 
      [NSString stringWithContentsOfFile:fileRoot 
       encoding:NSUTF8StringEncoding error:nil];

// first, separate by new line
NSArray* allLinedStrings = 
      [fileContents componentsSeparatedByCharactersInSet:
      [NSCharacterSet newlineCharacterSet]];

// then break down even further 
NSString* strsInOneLine = 
      [allLinedStrings objectAtIndex:0];

// choose whatever input identity you have decided. in this case ;
NSArray* singleStrs = 
      [currentPointString componentsSeparatedByCharactersInSet:
      [NSCharacterSet characterSetWithCharactersInString:@";"]];

そこにあります。

于 2010-12-17T01:23:10.480 に答える
64

それは素晴らしい質問です。@Diederikは良い答えを持っていると思いますが、Cocoa がまさにあなたがやりたいことのためのメカニズムを持っていないのは残念です。

NSInputStreamN バイトのチャンクを読み取ることができます ( と非常によく似てjava.io.BufferedReaderいます) が、それを自分で に変換してから、NSString改行 (またはその他の区切り文字) をスキャンし、次の読み取りのために残りの文字を保存するか、さらに文字を読み取る必要があります。改行がまだ読み取られていない場合。(NSFileHandleを読み取ってNSDataから に変換できますが、NSString基本的には同じプロセスです。)

Apple には、詳細を記入するのに役立つStream Programming Guideがあります。この SO の質問uint8_t*は、バッファを扱う場合にも役立ちます。

このような文字列を頻繁に (特にプログラムのさまざまな部分で) 読み取る場合は、詳細を処理できるクラスにこの動作をカプセル化するか、サブクラス化することをお勧めしますNSInputStream(サブクラス化された ) と、必要なものを正確に読み取ることができるメソッドを追加します。

記録として、これは追加するのに適した機能だと思います。これを可能にする機能の拡張リクエストを提出します。:-)


編集:このリクエストはすでに存在することが判明しました。これについては、2006 年からのレーダーがあります (Apple 社内の場合は rdar://4742914)。

于 2009-06-25T18:01:54.303 に答える
34

これでうまくいくはずです:

#include <stdio.h>

NSString *readLineAsNSString(FILE *file)
{
    char buffer[4096];

    // tune this capacity to your liking -- larger buffer sizes will be faster, but
    // use more memory
    NSMutableString *result = [NSMutableString stringWithCapacity:256];

    // Read up to 4095 non-newline characters, then read and discard the newline
    int charsRead;
    do
    {
        if(fscanf(file, "%4095[^\n]%n%*c", buffer, &charsRead) == 1)
            [result appendFormat:@"%s", buffer];
        else
            break;
    } while(charsRead == 4095);

    return result;
}

次のように使用します。

FILE *file = fopen("myfile", "r");
// check for NULL
while(!feof(file))
{
    NSString *line = readLineAsNSString(file);
    // do stuff with line; line is autoreleased, so you should NOT release it (unless you also retain it beforehand)
}
fclose(file);

このコードは、ファイルから一度に最大4095の非改行文字を読み取ります。4095文字を超える行がある場合は、改行またはファイルの終わりに達するまで読み取りを続けます。

:私はこのコードをテストしていません。使用する前にテストしてください。

于 2009-06-25T18:18:48.597 に答える
12

Mac OS X は Unix、Objective-C は C のスーパーfopenセットfgetsです<stdio.h>。動作することが保証されています。

[NSString stringWithUTF8String:buf]C 文字列を に変換しNSStringます。他のエンコーディングで文字列を作成する方法や、コピーせずに作成する方法もあります。

于 2010-08-03T23:30:36.753 に答える
9

NSInputStreamファイル ストリームの基本的な実装を持つ which を使用できます。バイトをバッファに読み込むことができます (read:maxLength:メソッド)。自分でバッファの改行をスキャンする必要があります。

于 2009-06-25T16:46:55.733 に答える
7

Cocoa/Objective-C でテキスト ファイルを読み取る適切な方法は、Apple の String プログラミング ガイドに記載されています。ファイルを読み書きするためのセクションは、まさにあなたが求めているものでなければなりません。PS: 「線」とは何ですか? "\n" で区切られた文字列の 2 つのセクション? それとも「\r」?それとも「\r\n」?それとも、実際に段落を求めているのでしょうか? 前述のガイドには、文字列を行または段落に分割するセクションも含まれています。(このセクションは「段落と改行」と呼ばれ、上で指摘したページの左側のメニューにリンクされています。残念ながら、このサイトでは複数の URL を投稿することはできません。まだ信頼できるユーザーではありません。)

クヌースの言葉を借りれば、時期尚早の最適化は諸悪の根源です。「ファイル全体をメモリに読み込む」のが遅いと単純に思い込まないでください。ベンチマークしましたか?実際にファイル全体をメモリに読み込むことを知っていますか? おそらく、単にプロキシ オブジェクトを返し、文字列を使用するときに舞台裏で読み取りを続けるのでしょうか? (免責事項:NSStringが実際にこれを行うかどうかはわかりません。おそらく可能です。)ポイントは、最初に文書化された方法で物事を行うことです。次に、ベンチマークがこれが望むパフォーマンスを持たないことを示している場合は、最適化します。

于 2009-06-26T23:04:35.100 に答える
6

これらの回答の多くは、コードの長いチャンクであるか、ファイル全体を読み取ります。私はまさにこのタスクに c メソッドを使用するのが好きです。

FILE* file = fopen("path to my file", "r");

size_t length;
char *cLine = fgetln(file,&length);

while (length>0) {
    char str[length+1];
    strncpy(str, cLine, length);
    str[length] = '\0';

    NSString *line = [NSString stringWithFormat:@"%s",str];        
    % Do what you want here.

    cLine = fgetln(file,&length);
}

fgetln は改行文字を保持しないことに注意してください。また、NULL 終端のためのスペースを作りたいので、str の長さを +1 します。

于 2016-03-07T19:50:09.820 に答える
4

他の人が答えているように、NSInputStream と NSFileHandle の両方が適切なオプションですが、NSData とメモリ マッピングを使用してかなりコンパクトな方法で実行することもできます。

BRLineReader.h

#import <Foundation/Foundation.h>

@interface BRLineReader : NSObject

@property (readonly, nonatomic) NSData *data;
@property (readonly, nonatomic) NSUInteger linesRead;
@property (strong, nonatomic) NSCharacterSet *lineTrimCharacters;
@property (readonly, nonatomic) NSStringEncoding stringEncoding;

- (instancetype)initWithFile:(NSString *)filePath encoding:(NSStringEncoding)encoding;
- (instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
- (NSString *)readLine;
- (NSString *)readTrimmedLine;
- (void)setLineSearchPosition:(NSUInteger)position;

@end

BRLineReader.m

#import "BRLineReader.h"

static unsigned char const BRLineReaderDelimiter = '\n';

@implementation BRLineReader
{
    NSRange _lastRange;
}

- (instancetype)initWithFile:(NSString *)filePath encoding:(NSStringEncoding)encoding
{
    self = [super init];
    if (self) {
        NSError *error = nil;
        _data = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedAlways error:&error];
        if (!_data) {
            NSLog(@"%@", [error localizedDescription]);
        }
        _stringEncoding = encoding;
        _lineTrimCharacters = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    }

    return self;
}

- (instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
{
    self = [super init];
    if (self) {
        _data = data;
        _stringEncoding = encoding;
        _lineTrimCharacters = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    }

    return self;
}

- (NSString *)readLine
{
    NSUInteger dataLength = [_data length];
    NSUInteger beginPos = _lastRange.location + _lastRange.length;
    NSUInteger endPos = 0;
    if (beginPos == dataLength) {
        // End of file
        return nil;
    }

    unsigned char *buffer = (unsigned char *)[_data bytes];
    for (NSUInteger i = beginPos; i < dataLength; i++) {
        endPos = i;
        if (buffer[i] == BRLineReaderDelimiter) break;
    }

    // End of line found
    _lastRange = NSMakeRange(beginPos, endPos - beginPos + 1);
    NSData *lineData = [_data subdataWithRange:_lastRange];
    NSString *line = [[NSString alloc] initWithData:lineData encoding:_stringEncoding];
    _linesRead++;

    return line;
}

- (NSString *)readTrimmedLine
{
    return [[self readLine] stringByTrimmingCharactersInSet:_lineTrimCharacters];
}

- (void)setLineSearchPosition:(NSUInteger)position
{
    _lastRange = NSMakeRange(position, 0);
    _linesRead = 0;
}

@end
于 2014-01-21T19:36:41.577 に答える
4

ファイルを 1 行ずつ読み取る (非常に大きなファイルの場合も同様) には、次の関数を使用できます。

DDFileReader * reader = [[DDFileReader alloc] initWithFilePath:pathToMyFile];
NSString * line = nil;
while ((line = [reader readLine])) {
  NSLog(@"read line: %@", line);
}
[reader release];

または:

DDFileReader * reader = [[DDFileReader alloc] initWithFilePath:pathToMyFile];
[reader enumerateLinesUsingBlock:^(NSString * line, BOOL * stop) {
  NSLog(@"read line: %@", line);
}];
[reader release];

これを可能にするクラス 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

実装 (.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

クラスはDave DeLongによって行われました

于 2013-01-17T13:53:56.160 に答える
1

この答えは ObjC ではなく C です。

ObjC は 'C' ベースなので、なぜ fgets を使用しないのでしょうか?

そして、はい、ObjC には独自のメソッドがあると確信しています。私はそれが何であるかを知るのにまだ十分に熟達していません :)

于 2009-06-25T15:19:34.963 に答える
0

@lukaswelte からの応答とDave DeLongからのコードは非常に役に立ちました。この問題の解決策を探していましたが、大きなファイルを解析する必要がありまし\r\n\n.

記述されたコードには、複数の文字で解析する場合のバグが含まれています。以下のようにコードを変更しました。

.h ファイル:

#import <Foundation/Foundation.h>

@interface FileChunkReader : 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

.m ファイル:

#import "FileChunkReader.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;
        }
    }

    if (foundRange.location != NSNotFound
        && length < foundRange.location + foundRange.length )
    {
        // if the dataToFind is partially found at the end of [self bytes],
        // then the loop above would end, and indicate the dataToFind is found
        // when it only partially was.
        foundRange.location = NSNotFound;
    }

    return foundRange;
}

@end

@implementation FileChunkReader

@synthesize lineDelimiter, chunkSize;

- (id) initWithFilePath:(NSString *)aPath {
    if (self = [super init]) {
        fileHandle = [NSFileHandle fileHandleForReadingAtPath:aPath];
        if (fileHandle == nil) {
            return nil;
        }

        lineDelimiter = @"\n";
        currentOffset = 0ULL; // ???
        chunkSize = 128;
        [fileHandle seekToEndOfFile];
        totalFileLength = [fileHandle offsetInFile];
        //we don't need to seek back, since readLine will do that.
    }
    return self;
}

- (void) dealloc {
    [fileHandle closeFile];
    currentOffset = 0ULL;

}

- (NSString *) readLine {
    if (currentOffset >= totalFileLength)
    {
        return nil;
    }

    @autoreleasepool {

        NSData * newLineData = [lineDelimiter dataUsingEncoding:NSUTF8StringEncoding];
        [fileHandle seekToFileOffset:currentOffset];
        unsigned long long originalOffset = currentOffset;
        NSMutableData *currentData = [[NSMutableData alloc] init];
        NSData *currentLine = [[NSData alloc] init];
        BOOL shouldReadMore = YES;


        while (shouldReadMore) {
            if (currentOffset >= totalFileLength)
            {
                break;
            }

            NSData * chunk = [fileHandle readDataOfLength:chunkSize];
            [currentData appendData:chunk];

            NSRange newLineRange = [currentData rangeOfData_dd:newLineData];

            if (newLineRange.location != NSNotFound) {

                currentOffset = originalOffset + newLineRange.location + newLineData.length;
                currentLine = [currentData subdataWithRange:NSMakeRange(0, newLineRange.location)];

                shouldReadMore = NO;
            }else{
                currentOffset += [chunk length];
            }
        }

        if (currentLine.length == 0 && currentData.length > 0)
        {
            currentLine = currentData;
        }

        return [[NSString alloc] initWithData:currentLine encoding:NSUTF8StringEncoding];
    }
}

- (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
于 2016-02-19T16:31:24.017 に答える
0

私が試した他のすべての回答が何らかの形で不足したため、これを追加しています。次のメソッドは、大きなファイル、任意の長い行、および空の行を処理できます。実際のコンテンツでテストされており、出力から改行文字が削除されます。

- (NSString*)readLineFromFile:(FILE *)file
{
    char buffer[4096];
    NSMutableString *result = [NSMutableString stringWithCapacity:1000];

    int charsRead;
    do {
        if(fscanf(file, "%4095[^\r\n]%n%*[\n\r]", buffer, &charsRead) == 1) {
            [result appendFormat:@"%s", buffer];
        }
        else {
            break;
        }
    } while(charsRead == 4095);

    return result.length ? result : nil;
}

クレジットは@Adam Rosenfieldと@sooopに行きます

于 2018-02-08T09:35:59.600 に答える
0

カテゴリや拡張子を使用して、私たちの生活を少し楽にします。

extension String {

    func lines() -> [String] {
        var lines = [String]()
        self.enumerateLines { (line, stop) -> () in
            lines.append(line)
        }
        return lines
    }

}

// then
for line in string.lines() {
    // do the right thing
}
于 2015-06-07T04:57:33.977 に答える
-2

これは、小さなファイルに使用するシンプルなソリューションです。

NSString *path = [[NSBundle mainBundle] pathForResource:@"Terrain1" ofType:@"txt"];
NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil];
NSArray *lines = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\r\n"]];
for (NSString* line in lines) {
    if (line.length) {
        NSLog(@"line: %@", line);
    }
}
于 2011-06-25T08:21:39.587 に答える
-7

このスクリプトを使用すると、うまく機能します。

NSString *path = @"/Users/xxx/Desktop/names.txt";
NSError *error;
NSString *stringFromFileAtPath = [NSString stringWithContentsOfFile: path
                                                           encoding: NSUTF8StringEncoding
                                                              error: &error];
if (stringFromFileAtPath == nil) {
    NSLog(@"Error reading file at %@\n%@", path, [error localizedFailureReason]);
}
NSLog(@"Contents:%@", stringFromFileAtPath);
于 2010-09-20T22:42:38.780 に答える