iOS基础之文件读写

本文详细介绍了在iOS开发中如何进行文件读写操作,包括字符串、数组、字典和数据类型的读写,并讲解了如何使用NSFileManager进行文件管理,如创建、移动、复制、删除和读取文件等。同时,还涵盖了复杂对象的存储和恢复方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.简单对象的读写(I/O)操作
iOS中提供四种类型可以直接进行文件文件存取:NSString(字符串), NSArray(数组), NSDictionary(字典), NSData(数据) ~~~~以上类型包括子类

读取
写入
NSString/NSMutableString
stringWithContentsOfFile: encoding: error:
writeToFile: atomically: encoding: error: 
NSArray/NSMutableArray
arrayWithContentsOfFile:
writeToFile: atomically:
NSDictionary/NSMutableDictionary
dictionaryWithContentsOfFile:
writeToFile: atomically:
NSData/NSMutableData
dataWithContentsOfFile:
writeToFile: atomically:

1.字符串

字符串的写入
①.找到Document的路径
``` python
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];   
```
②. 拼接要写入的路径   
NSString *filePath = [documentPath stringByAppendingPathComponent:@"file.txt"];    NSString *writeStr = @"you are great";    
③.把writeStr写入文件   
参数1: 写入文件的路径
参数2: 写成YES之后, 在一些极端环境下可以起到保护作用, YES相当于把数据写到一个新的文件当中, 成功之后, 系统帮我们完成替换, 写成NO相当于直接向文件中写(写之前把原来的数据清空)
参数3: 想要捕获error, 先声明一个error对象 
NSError *error = nil;   
BOOL writeResult = [writeStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];   
if (writeResult == YES) {
NSLog(@"写入成功");
} else {       
NSLog(@"error = %@", error);   
}

字符串的读取
###
NSString *readStr= [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];    NSLog(@"readStr = %@", readStr);

2.数组

数组的写入
NSArray *array = @[@"jiege", @"linger"];   
BOOL arrayResult = [array writeToFile:arrayPath atomically:YES];   
if (arrayResult) {        NSLog(@"写入成功");    } else{        NSLog(@"写入失败");    }

数组的读取
NSArray *readArray = [NSArray arrayWithContentsOfFile:arrayPath];   
NSLog(@"readArray = %@", readArray);

3.字典

字典的写入
NSDictionary *dic = @{@"name":@“杰哥",@"sex":@“男"};   
NSString *dicPath = [documentPath stringByAppendingPathComponent:@"dicFile.plist"];    BOOL dicResult = [dic writeToFile:dicPath atomically:YES];   
if (dicResult) {        NSLog(@"字典写入成功");    } else {        NSLog(@"字典写入失败");    }

字典的读取
NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];    NSLog(@"readDic = %@", readDic);

4.数据

写入文件

①.创建UIImage对象
UIImage *image = [UIImage imageNamed:@“1.png"];
②.图片写入沙盒, 先把图片转换成NSData ( 参数1: image    参数2: 压缩比例)   
NSData *imgData = UIImageJPEGRepresentation(houImage, 1.0);   
③.构造二级制文件的存储路径  
NSString *saveImagePath = [documentPath stringByAppendingPathComponent:@"hou.jpg"];    ④.把数据写入沙盒, 进行持久化       
BOOL imgResult = [imgData writeToFile:saveImagePath atomically:YES];       
if (imgResult) {  NSLog(@"成功");  } else {            NSLog(@"失败");        }    
   
从沙盒中读取数据   
NSData *readData = [NSData dataWithContentsOfFile:saveImagePath];   
UIImage *image = [UIImage imageWithData:readData];   
NSLog(@"---%@", image);

二.文件管理器与文件对接器
1.文件管理器(NSFileManager)
此类主要是对文件进行的操作(文件的创建, 移动, 复制,  删除, 改名, 获取文件的属性等)
功能:
①. 创建文件管理器对象
NSFileManager *fileManager = [NSFileManager defaultManager];
②. 创建一个文件并写入数据
方法名 - (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)dic;
例: [fileManager createFileAtPath:createFilePath contents:strData attributes:nil]
③. 判断改路径上是否存在文件
BOOL isExit = [fileManager fileExistsAtPath:createFilePath];
④. 从一个文件中读取数据
- (NSData *)contentsAtPath:(NSString *)path
⑤. srcPath路径上的文件移动到dstPath路径上(这里的路径是文件路径而不是目录)
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;
⑥. srcPath路径上的文件赋值到dstPath路径上
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;
⑦. 比较两个文件的内容是否一样
- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2
⑧. 移除文件
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error;
⑨. 创建文件夹
- (BOOL)createDirectoryAtPath: withIntermediateDirec

例:

 1.文件的创建    
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];    NSLog(@"%@", cachePath);    
2.拼接想创建文件的路径    
NSString *createFilePath = [cachePath stringByAppendingPathComponent:@"createFile.txt"]; 
3.创建文件管理器对象(单例的创建方法)    
NSFileManager *fileManager = [NSFileManager defaultManager];    
4.判断该路径上有没有文件   
 BOOL isExit = [fileManager fileExistsAtPath:createFilePath];    
 if (isExit) { NSLog(@"该路径上已经存在文件"); } 
 else {  NSString *writeStr = @"I Love You";    
           字符串转NSData类型        
           NSData *strData = [writeStr dataUsingEncoding:NSUTF8StringEncoding];       
          创建文件
         (参数1: 文件路径  参数2: 创建文件时, 想写入的内容 参数3: 对文件的一些特殊性设置 (读写性, 权限设置), 当写成nil, 就是使用系统的默认设置)        BOOL createResult = [fileManager createFileAtPath:createFilePath contents:strData attributes:nil];        
  if (createResult) { NSLog(@"创建成功"); } else {] NSLog(@"创建失败"); }   
 }


三.复杂对象的写入和读取


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值