一个很长很长的文件读取
我想读取一个文本文件并对该数据执行一些操作。
文件大小为 67 mb
我如何读取。
文件为文本格式。
它在模拟器中工作,但在设备中发出内存警告并崩溃。
代码
NSString *content = [[[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil] autorelease];
当这句话完成时,
崩溃了。谢谢,
夏姆·帕尔玛
I want to read one text file and perform some action on that data.
file size is 67 mb
how can i read.
file is in text format.
its working in simulateor but giving memory warning in device and crashes.
code is
NSString *content = [[[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil] autorelease];
crashes when this sentence complete.
Thanks,
Shyam parmar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还没有给出代码,但如果您使用 stringWithContentsOfFile 来获取整个文件,请考虑使用 NSInputStream 或 stdio 来读取它并以增量方式处理或显示它。
You haven't given code, but if you are using
stringWithContentsOfFile
to get an entire file consider using NSInputStream or stdio to read it and process or display it more incrementally.快速通道,试试彼得的建议。问题是一次性加载所有内容,因为我相信您自己的应用程序大约有 20 MB 可用空间。如果您使用 NSInputStream,则可以分段加载它,因此您不会立即填满整个内存。另请阅读另一个问题的答案:Objective-C:逐行读取文件
Fasttracks, try what Peter suggested. The problem is loading it all at once, since you have about 20 MB available for your own app, i believe. If you'd use a NSInputStream, you can load it in pieces, due to which you wont fill up the entire memory at once. Also read this answer to another question: Objective-C: Reading a file line by line
您是否从
- (void) viewDidLoad
开始读取文件?这可能就是问题所在。尝试在不同的线程中开始读取它,例如:[self PerformSelectorInBackground:@selector(method) withObject:nil];
Do you start reading the file from
- (void) viewDidLoad
? That might be the problem. Try start reading it in a different thread, like:[self performSelectorInBackground:@selector(method) withObject:nil];