如何从巨大的文本文件中读取文本块?

发布于 2024-12-05 12:16:25 字数 142 浏览 0 评论 0原文

我正在尝试读取包含数十亿字符的文本文件。使用功能 contentOfFile 无法正常工作,因为我的应用程序因此而崩溃。

因此,任何人请向我发送示例代码,以便我根据我的要求获得块。无论我需要哪一个,我只想得到那个。 请尽快回复。

I am trying to read a text file containing characters in billions. Using the function
contentOfFile is not working, as my application get crashed due to it.

So anybody please send me the sample code so that I get the chunks according to my requirement.Whichever i need i wanna get that one only.
please reply as soon as possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁许谁一生繁华 2024-12-12 12:16:25

我猜这是一个 iOS 应用程序。在这种情况下,您可能会通过调用 contentsOfFile: 达到内存限制,因为该方法试图将文件的全部内容读入变量(内存)。请记住,在 iOS 上,您的应用程序必须运行良好,如果它决定消耗太多内存,那么看门狗进程将终止您的应用程序,以防止设备重新启动(发生这种情况是因为 iOS 设备上没有可交换的磁盘)。

你看过NSFileHandle吗? NSFileHandle 支持在文本文件中查找。通过一些简单的迭代,您可以使用以下方法在文件中查找并读取数据块:

- (NSData *)readDataOfLength:(NSUInteger)length;
- (void)seekToFileOffset:(unsigned long long)offset;

它可能看起来像这样。假设 pathToFile 是一个 NSString,包含要读入的文本文件的路径。

uint64 offset = 0;
uint32 chunkSize = 1024;     //Read 1KB chunks.
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:pathToFile];
NSData *data = [handle readDataOfLength:chunkSize];

while ([data length] > 0)
{
     //Make sure for the next line you choose the appropriate string encoding.
     NSString *dataString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

     /* PERFORM STRING PROCESSING HERE */

     /* END STRING PROCESSING */
     offset += [data length];

     [handle seekToFileOffset:offset];
     data = [handle readDataOfLength:chunkSize];
}

[handle closeFile];

I'm guessing this is an iOS app. In that case, you are likely hitting the memory limit by calling contentsOfFile: because that method is trying to read the entire contents of the file into a variable (memory). Remember that on iOS your app must play nice and if it decides to consume too much memory, then the watchdog process will kill your app to save the device from rebooting (which happens because there is no disk to swap to on iOS devices).

Have you had a look at NSFileHandle? NSFileHandle supports seeking within a text a file. With some simple iteration you can use the following to methods to seek within the file and read chunks of data:

- (NSData *)readDataOfLength:(NSUInteger)length;
- (void)seekToFileOffset:(unsigned long long)offset;

It might look something like this. Assume pathToFile is an NSString containing the path to the text file to be read in.

uint64 offset = 0;
uint32 chunkSize = 1024;     //Read 1KB chunks.
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:pathToFile];
NSData *data = [handle readDataOfLength:chunkSize];

while ([data length] > 0)
{
     //Make sure for the next line you choose the appropriate string encoding.
     NSString *dataString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

     /* PERFORM STRING PROCESSING HERE */

     /* END STRING PROCESSING */
     offset += [data length];

     [handle seekToFileOffset:offset];
     data = [handle readDataOfLength:chunkSize];
}

[handle closeFile];
_失温 2024-12-12 12:16:25

一个好主意是查看 textedit 源代码,因为我之前用它打开过大量文件,应该有办法做到这一点。但不确定为什么你的应用程序崩溃了。应该没有问题。

A good idea is to look at the textedit source because I've opened massive files with it before and there should be a way to do it. Not sure why your app is crashing though. It shouldn't have a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文