//init方法、便利构造器 这是OC中初始化对象的两种方法
NSString *path1= @"/Users/1010/Desktop/abc.txt";
//用一个文件来初始化一个字符串,也就是读文件中的内容
NSError *error = nil;
// NSString *content1 = [[NSString alloc]initWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:&error];
NSString *content1 = [NSString stringWithContentsOfFile:path1 encoding:4 error:&error];
if (error != nil)
{
NSLog(@"file:%@",[error localizedFailureReason]);
}
NSLog(@"content1 = %@",content1);
//写文件,它会覆盖掉文件中以前的内容
NSString *str1 = @"i am learning ios";
BOOL s = [str1 writeToFile:path1
atomically:YES
encoding:4
error:&error];//atomically 若为no,则str1会立刻写入到文件中,若为YES,则先写入到缓存中,当该文件为空闲的时候,在从缓存写入到文件中
NSLog(@"s = %d",s);
// NSData *data1 = [[NSData alloc]initWithContentsOfFile:path1];//init初始化
NSData *data1 = [NSData dataWithContentsOfFile:path1];//便利构造器初始化
NSLog(@"data1 = %@",data1);
NSString *str2 = [[NSString alloc]initWithData:data1 encoding:4];//encoding:4,4代表UTF-8编码
NSLog(@"str2 = %@",str2);
NSString *path2 = @"http://www.baidu.com/img/bdlogo.gif";//网络上的一张图片的地址
NSURL *url = [NSURL URLWithString:path2];//将字符串转换成url
NSData *imgData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:imgData];
[imgData writeToFile:@"/Users/1010/Desktop/logo.gif" atomically:YES]; //NSData虽然是二进制的,但是可以写入到文件中
CGRect r = CGRectMake(100, 100, 100, 100);//ios中的屏幕是绝对定位
UIImageView *_imgView = [[UIImageView alloc]initWithFrame:r];
_imgView.image = img;
[self.window addSubview:_imgView];
//文件管理
NSFileManager *fm = [NSFileManager defaultManager];//文件管理工具是一个单例
// [fm createDirectoryAtPath:@"/Users/1010/Desktop/abc/123" withIntermediateDirectories:YES attributes:nil error:nil];
//withIntermediateDirectories 是否创建中间目录123
//attributes文件袋额属性,只读等等权限一类的东西
// [fm createFileAtPath:@"/Users/1010/Desktop/abc/123/logo.gif" contents:imgData attributes:nil];//在某个路径下创建内容为.....的文件
// [fm removeItemAtPath:@"/Users/1010/Desktop/abc/123/logo.gif" error:nil];
NSString *oriPath = @"/Users/1010/Desktop/abc/123/logo.gif";
NSString *targetPath = @"/Users/1010/Desktop/abc/logo.gif";
[fm moveItemAtPath:oriPath toPath:targetPath error:nil];
//
NSString *bundleStr = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"txt"];//mainBundle这是一个单例
NSLog(@"bundleStr = %@",bundleStr);