Objective-C:逐行读取文件 [英] Objective-C: Reading a file line by line

查看:580
本文介绍了Objective-C:逐行读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中处理大型文本文件的适当方法是什么?假设我需要分别读取每一行,并将每行视为一个NSString。这样做的最有效的方法是什么?



一个解决方案是使用NSString方法:

pre > +(id)stringWithContentsOfFile:(NSString *)path
encoding:(NSStringEncoding)enc
错误:(NSError **)错误

,然后用换行符分隔线,然后迭代数组中的元素。但是,这似乎相当低效。有没有简单的方法来对待文件作为一个流,枚举每一行,而不是一次只读一次?有点像Java的java.io.BufferedReader。

解决方案

这是一个很好的问题。我认为 @Diederik 有一个很好的答案,尽管不幸的是Cocoa没有一个机制来完成你想要做的事情。



NSInputStream 允许您读取N个字节的块(非常类似于 java.io.BufferedReader ),但您必须将其转换为 NSString然后扫描换行符(或任何其他分隔符),并保存下一次读取的剩余字符,或者如果还没有读取换行符,请阅读更多字符。 ( NSFileHandle 可以读取 NSData ,然后转换为 NSString ,但它本质上是相同的过程。)



苹果有一个流编程指南,可以帮助填写详细信息,和这个SO问题也可以帮助如果你打算处理 uint8_t * 缓冲区。



如果你打算读取字符串像这样的经常(特别是在你的程序的不同部分),将这种行为封装在一个可以处理细节的类中,或者甚至子类化 NSInputStream (这是设计分类)和添加方法允许你正确地阅读你想要的东西。

为了记录,我认为这将是一个很好的功能添加,我会提交一个增强请求的东西这使得这成为可能。 : - )




编辑:原来这个请求已经存在。有一个从2006年开始的雷达(对于苹果公司内部的人来说,rdar:// 4742914)。


What is the appropriate way of dealing with large text files in Objective-C? Let's say I need to read each line separately and want to treat each line as an NSString. What is the most efficient way of doing this?

One solution is using the NSString method:

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

and then split the lines with a newline separator, and then iterate over the elements in the array. However, this seems fairly inefficient. Is there no easy way to treat the file as a stream, enumerating over each line, instead of just reading it all in at once? Kinda like Java's java.io.BufferedReader.

解决方案

That's a great question. I think @Diederik has a good answer, although it's unfortunate that Cocoa doesn't have a mechanism for exactly what you want to do.

NSInputStream allows you to read chunks of N bytes (very similar to java.io.BufferedReader), but you have to convert it to an NSString on your own, then scan for newlines (or whatever other delimiter) and save any remaining characters for the next read, or read more characters if a newline hasn't been read yet. (NSFileHandle lets you read an NSData which you can then convert to an NSString, but it's essentially the same process.)

Apple has a Stream Programming Guide that can help fill in the details, and this SO question may help as well if you're going to be dealing with uint8_t* buffers.

If you're going to be reading strings like this frequently (especially in different parts of your program) it would be a good idea to encapsulate this behavior in a class that can handle the details for you, or even subclassing NSInputStream (it's designed to be subclassed) and adding methods that allow you to read exactly what you want.

For the record, I think this would be a nice feature to add, and I'll be filing an enhancement request for something that makes this possible. :-)


Edit: Turns out this request already exists. There's a Radar dating from 2006 for this (rdar://4742914 for Apple-internal people).

这篇关于Objective-C:逐行读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆