iphone 读取 一行文件内…

来源:互联网 发布:淘宝全屏导航栏颜色 编辑:程序博客网 时间:2024/05/22 01:25

第一种:

NSStringEncoding encoder =NSUTF8StringEncoding;

NSString *file = [NSStringstringWithContentsOfFile:@"/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 = [filelineRangeForRange:line];

NSString *content = [filesubstringWithRange: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 = [linesobjectEnumerator];

 

//读取<>里的内容

while(tmp = [nse nextObject]) {

NSString *stringBetweenBrackets =nil;

NSScanner *scanner = [NSScannerscannerWithString:tmp];

[scannerscanUpToString:@"<" intoString:nil];

[scannerscanString:@"<" intoString:nil];

[scannerscanUpToString:@">"intoString:&stringBetweenBrackets];

NSLog([stringBetweenBracketsdescription]);

}

0 0