1

我正在尝试从文本文件中读取文本。我使用 stringWithContentsOfFile 来做到这一点。

pathName        = [[NSBundle mainBundle] pathForResource:sample ofType:@"txt"];
entireContent   = [NSString stringWithContentsOfFile:pathName encoding:NSUTF8StringEncoding error:NULL];
entireChapter   = [entireContent substringWithRange:sourceRange];

你们有没有其他方法可以快速获得字符串?

4

1 回答 1

1

你可以使用 NSFileHandle。

NSFileHandle *fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fileHandler seekToFileOffset:10];
NSData *data = [fileHandler readDataOfLength:10];
[fileHandler closeFile];

你可以试试这个。然后您可以将 nsdata 转换为字符串。

于 2011-09-09T09:42:36.970 回答