读取文件内容&文件管理

1、 Resource 读取文件内容
 NSBundle *bundel=[NSBundle mainBundle];
 NSString *filePath=[bundel pathForResource:@"txtfile" ofType:@""];
 NSLog(@"filePath:%@",filePath);
 NSString *fileContent=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
 NSLog(@"fileContent:%@",fileContent); 

2、读取图片

 NSBundle *bundel=[NSBundle mainBundle];
 NSString *imgPath=[bundel pathForResource:@"home" ofType:@"png"];
 UIImage *image=[UIImage imageWithContentsOfFile:imgPath];
 UIImageView *imageView =[[UIImageView alloc]initWithImage:image];
 [self.view addSubview:imageView];
    
 //简便方法
 UIImageView *imageView2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"list"]];
 //move position
 imageView2.center=CGPointMake(40, 15);
 [self.view addSubview:imageView2];

3、NSFileManager 打印目录信息

 NSFileManager *fm=[NSFileManager defaultManager];
 NSString *path=[[NSBundle mainBundle]bundlePath];
 NSArray *info=[fm contentsOfDirectoryAtPath:path error:nil];
 NSLog(@"list:%@",info);


你可能感兴趣的:(pathForResource)