最近在接手一个比较老的混合开发的项目,发现有很多加载本地资源的js脚本文件,特此记录一下加载资源的几种方法;
另外,我的简书地址是:https://www.jianshu.com/u/63d3f119c4d0
//第一种方法: 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类方法读取的内容是: %@",content); //第四种方法: NSFileHandle实例方法读取内容 NSFileHandle* fh = [NSFileHandle fileHandleForReadingAtPath:thepath]; data = [fh readDataToEndOfFile]; NSLog(@"NSFileHandle实例读取的内容是: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); } //加载bund里面的文件 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"LMSetting" ofType:@"plist"];