ios关于文件的读取
3个回答
2015-10-11 · 知道合伙人互联网行家
关注
展开全部
ios关于文件的读取有四种方法:
#import <foundation foundation.h="">
int main(int argc, const char * argv[])
{
@autoreleasepool
{
//第一种方法: NSFileManager实例方法读取数据
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
NSString* thepath = [paths lastObject];
thepath = [thepath stringByAppendingPathComponent:@"fd_list.txt"];
NSLog(@"桌面目录:%@", thepath);
NSFileManager* fm = [NSFileManager defaultManager];
NSData* data = [[NSData alloc] init];
data = [fm contentsAtPath:thepath];
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//第二种方法: NSData类方法读取数据
data = [NSData dataWithContentsOfFile:thepath];
NSLog(@"NSData类方法读取的内容是:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//第三种方法: NSString类方法读取内容
NSString* content = [NSString stringWithContentsOfFile:thepath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"NSString类方法读取的内容是:\n%@",content);
//第四种方法: NSFileHandle实例方法读取内容
NSFileHandle* fh = [NSFileHandle fileHandleForReadingAtPath:thepath];
data = [fh readDataToEndOfFile];
NSLog(@"NSFileHandle实例读取的内容是:\n%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
return 0;
}
</foundation>
#import <foundation foundation.h="">
int main(int argc, const char * argv[])
{
@autoreleasepool
{
//第一种方法: NSFileManager实例方法读取数据
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
NSString* thepath = [paths lastObject];
thepath = [thepath stringByAppendingPathComponent:@"fd_list.txt"];
NSLog(@"桌面目录:%@", thepath);
NSFileManager* fm = [NSFileManager defaultManager];
NSData* data = [[NSData alloc] init];
data = [fm contentsAtPath:thepath];
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//第二种方法: NSData类方法读取数据
data = [NSData dataWithContentsOfFile:thepath];
NSLog(@"NSData类方法读取的内容是:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//第三种方法: NSString类方法读取内容
NSString* content = [NSString stringWithContentsOfFile:thepath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"NSString类方法读取的内容是:\n%@",content);
//第四种方法: NSFileHandle实例方法读取内容
NSFileHandle* fh = [NSFileHandle fileHandleForReadingAtPath:thepath];
data = [fh readDataToEndOfFile];
NSLog(@"NSFileHandle实例读取的内容是:\n%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
return 0;
}
</foundation>
展开全部
NSString* path = [[NSBundle mainBundle] pathForResource:@"test"
ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSArray *arr = [content componentsSeparatedByString:@" "];
ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSArray *arr = [content componentsSeparatedByString:@" "];
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是要读取成5712101430 ?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询