简单对象写入文件
2015-09-28 20:22
281 查看
#pragma mark 写入文件
//简单对象写入文件---------------->字符串、数组、字典、图片(才怪,是把图片转换成的NSData对象)
//1.字符串
NSString *incantation =
@"yi hu shi ";
NSString *homePath =
NSHomeDirectory();
homePath = [homePath
stringByAppendingString:@"/hh.txt"];
//NO:不安全,会中断。
//AllowLossy允许丢失碎片。
//ExternalRepresentation按字节转换,一个字节也不能丢失。
[incantation writeToFile:homePath
atomically:YES
encoding:NSStringEncodingConversionExternalRepresentation
error:nil];
NSLog(@"%@",homePath);
//从文件获取字符串
NSString *result = [NSString
stringWithContentsOfFile:homePath encoding:NSStringEncodingConversionExternalRepresentation
error:nil];
NSLog(@"%@",result);
//2、数组写入文件
NSArray *array =
@[@"you",@"are",@"you"];
NSString *path = NSHomeDirectory();
path = [path stringByAppendingString:@"hitu"];
//写入
[array writeToFile:path
atomically:YES];
NSLog(@"%@",path);
//读取
NSArray *resultArray = [NSArray
arrayWithContentsOfFile:path];
NSLog(@"%@",resultArray);
//3、字典写入文件
NSDictionary *dictionary =
@{
@"yiyi":@"huhu",
@"yoyo":@"kiki"
};
NSString *path = NSHomeDirectory();
path = [path stringByAppendingString:@"/oppo.txt"];
//写入
[dictionary
writeToFile:path atomically:YES];
NSLog(@"%@",path);
//读取
NSDictionary *dict = [NSDictionary
dictionaryWithContentsOfFile:path];
NSLog(@"%@",dict);
//4、图片是以NSData写入文件
UIImage *image = [UIImage
imageNamed:@"background.png"];
//0-1之间的,1是原封不动的大小
NSData *data =
UIImageJPEGRepresentation(image, 0.5);
NSString *path = NSHomeDirectory();
path = [path stringByAppendingString:@"/image"];
//写入
[data writeToFile:path
atomically:YES];
NSLog(@"%@",path);
//读取
NSData *resultData = [NSData
dataWithContentsOfFile:path];
UIImage *resultImage = [UIImage
imageWithData:resultData];
[_imageView
setImage:resultImage];
//简单对象写入文件---------------->字符串、数组、字典、图片(才怪,是把图片转换成的NSData对象)
//1.字符串
NSString *incantation =
@"yi hu shi ";
NSString *homePath =
NSHomeDirectory();
homePath = [homePath
stringByAppendingString:@"/hh.txt"];
//NO:不安全,会中断。
//AllowLossy允许丢失碎片。
//ExternalRepresentation按字节转换,一个字节也不能丢失。
[incantation writeToFile:homePath
atomically:YES
encoding:NSStringEncodingConversionExternalRepresentation
error:nil];
NSLog(@"%@",homePath);
//从文件获取字符串
NSString *result = [NSString
stringWithContentsOfFile:homePath encoding:NSStringEncodingConversionExternalRepresentation
error:nil];
NSLog(@"%@",result);
//2、数组写入文件
NSArray *array =
@[@"you",@"are",@"you"];
NSString *path = NSHomeDirectory();
path = [path stringByAppendingString:@"hitu"];
//写入
[array writeToFile:path
atomically:YES];
NSLog(@"%@",path);
//读取
NSArray *resultArray = [NSArray
arrayWithContentsOfFile:path];
NSLog(@"%@",resultArray);
//3、字典写入文件
NSDictionary *dictionary =
@{
@"yiyi":@"huhu",
@"yoyo":@"kiki"
};
NSString *path = NSHomeDirectory();
path = [path stringByAppendingString:@"/oppo.txt"];
//写入
[dictionary
writeToFile:path atomically:YES];
NSLog(@"%@",path);
//读取
NSDictionary *dict = [NSDictionary
dictionaryWithContentsOfFile:path];
NSLog(@"%@",dict);
//4、图片是以NSData写入文件
UIImage *image = [UIImage
imageNamed:@"background.png"];
//0-1之间的,1是原封不动的大小
NSData *data =
UIImageJPEGRepresentation(image, 0.5);
NSString *path = NSHomeDirectory();
path = [path stringByAppendingString:@"/image"];
//写入
[data writeToFile:path
atomically:YES];
NSLog(@"%@",path);
//读取
NSData *resultData = [NSData
dataWithContentsOfFile:path];
UIImage *resultImage = [UIImage
imageWithData:resultData];
[_imageView
setImage:resultImage];
相关文章推荐
- ios-手势总结-清扫手势
- MYSQL数据操作表
- C. Beautiful Numbers
- C++拷贝构造函数
- Servlet中Service方法
- Android 从底层实现让应用杀不死(1)
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装
- java 心跳机制
- 获取 沙盒主路径 及其他下面的文件路径
- java基础之集合List、Set、Map的区别
- iOS界面编程-UILabel
- http://kb.cnblogs.com/page/196448/
- MySQL学习笔记之视图
- 19、linux软件管理之简述
- java 时间转换 将 util 时间转成 sql 时间
- Cannot read property 'root' of null 解决方法
- 跑马
- JAVA对文件的读操作总结
- 使用StatFs用于获取系统/sdcard存储空间信息 api18中增加的方法
- HDU 5475 An easy problem