您的位置:首页 > 其它

iphone 读取 一行文件内容的两种方法

2012-05-14 11:35 447 查看
第一种:

NSStringEncoding encoder = NSUTF8StringEncoding;

NSString *file = [NSString stringWithContentsOfFile:@"/Users/bjimac/Desktop/528.edl" usedEncoding:&encoder

error:nil];

NSRange line;

line.location = 0;

line.length = 1;

int length = [file length];

while (line.location <= length)

{

NSRange newLine = [file lineRangeForRange:line];

NSString *content = [file substringWithRange:newLine];

line.length = 1;

line.location = newLine.location + newLine.length + 1;

}

- (NSString *)substringWithRange:(NSRange)aRange ----该函数会返回包含aRange的行


所以上面代码中content就得到每一行的内容了。

第二种:

读取一般性文档文件

NSString *tmp;

NSArray *lines; /*将文件转化为一行一行的*/

lines = [[NSString    stringWithContentsOfFile:@"testFileReadLines.txt"]

componentsSeparatedByString:@"\n"];

NSEnumerator *nse = [lines objectEnumerator];

// 读取<>里的内容

while(tmp = [nse nextObject]) {

NSString *stringBetweenBrackets = nil;

NSScanner *scanner = [NSScanner scannerWithString:tmp];

[scanner scanUpToString:@"<" intoString:nil];

[scanner scanString:@"<" intoString:nil];

[scanner scanUpToString:@">" intoString:&stringBetweenBrackets];

NSLog([stringBetweenBrackets description]);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: