摘要:本文将带你了解IOS开发入门iOS 文件操作,希望本文对大家学IOS有所帮助。
本文将带你了解IOS开发入门iOS 文件操作,希望本文对大家学IOS有所帮助。
文件的基本操作
//NSFileManager 文件管理器类 用于操作文件或目录
?12NSFileManager *file = [NSFileManager defaultManager];NSFileManager
*file2 = [NSFileManager defaultManager];
//通过验证NSFileManager是一个单例类
?1NSLog(@"file=%p,file2=%p",file,file2);
//1. 遍历文件路径: 浅度遍历、深度遍历
?1234567891011121314151617//a. 浅度遍历: 只遍历当前这一层的文件夹的所有文件或目录NSError *error =
nil;NSArray *arr = [file contentsOfDirectoryAtPath:PATH
error:&error];if(error){
//判断如果error有值,则打印错误 NSLog(@"error =
%@",error);}else{
//没有错误,则打印获取的信息 NSLog(@"arr =
%@",arr);} //b.深度遍历:遍历当前层与当前层下的子层arr = [file
subpathsOfDirectoryAtPath:PATH error:&error];if(error){ NSLog(@"error
= %@",error);}else{ NSLog(@"arr =
%@",arr);}
//2.创建文件或目录
?123456789101112131415//a. 创建文件路径:
createFileAtPath //判断文件是否已存在:fileExistsAtPathBOOL bval = [file
fileExistsAtPath:CREATE_FILE];if(bval){ //返回值:YES 文件存在 NO
不存在 NSLog(@"文件已存在");}else{ //参数1:文件路径
参数2:内容(nil) 参数3:属性(nil) bval = [file
createFileAtPath:CREATE_FILE contents:nil
attributes:nil]; if(bval){ //返回值: YES
成功 NO
失败 NSLog(@"文件创建成功"); }else{ NSLog(@"文件创建失败"); }}
//b.创建目录路径:
?123456789101112bval = [file fileExistsAtPath:CREATE_PATH];if(bval){
//返回值:YES 目录存在 NO
不存在 NSLog(@"目录已存在");}else{ //参数1:路径
参数2:中间路径(YES) 参数3:属性(nil) 参数4:错误 bval = [file
createDirectoryAtPath:CREATE_PATH withIntermediateDirectories:YES
attributes:nil error:nil]; if(bval){ NSLog(@"目录创建成功"); }else{ NSLog(@"目录创建失败"); }}
//获取路径的属性
?12345678910111213141516171819NSDictionary *dic = [file
attributesOfItemAtPath:CREATE_FILE error:nil];NSLog(@"dic =
%@",dic); //NSNumber: 数字类 专门存储数字的类NSNumber *numA =
dic[@"NSFileSize"]; NSNumber *numB = @5;
//直接初始化数字类 NSLog(@"fileSize = %@,numB =
%@",numA,numB); //间接初始化数字类:
对象方法、类型方法 //通过一个整型,初始化数字类对象方法NSNumber *numC = [[NSNumber alloc]
initWithInt:6];NSLog(@"numC=%@",numC); //通过一个浮点型,初始化数字类类方法NSNumber
*numD = [NSNumber numberWithFloat:4.5];NSLog(@"numD=%@",numD);
文件的拷贝 移动 删除
?123NSFileManager *file = [NSFileManager
defaultManager]; //参数1:源路径 参数2: 目标路径 参数3:错误提示
//1. 文件或目录的拷贝:
?123456789101112131415//拷贝时:源文件存在,目标文件不存在,才能拷贝成功NSError *error =
nil; if([file
fileExistsAtPath:DST_PATH]){ NSLog(@"目标文件存在,无须拷贝");}else{ [file
copyItemAtPath:SRC_PATH toPath:DST_PATH
error:&error]; if(error){ NSLog(@"error
=
%@",error); }else{ NSLog(@"拷贝成功"); }}
//2. 文件或目录的移动:
?12345678910111213//移动时:源文件存在,目标文件不存在,才能移动成功 if([file
fileExistsAtPath:SRC_PATH]&&![file
fileExistsAtPath:DST_PATH]){ [file
moveItemAtPath:SRC_PATH toPath:DST_PATH
error:&error]; if(error){ NSLog(@"error
=
%@",error); }else{ NSLog(@"文件的移动成功"); }}else{ NSLog(@"不能确保源文件存在,目标文件不存在");}
//3. 删除
?1234567//删除时:指定的文件存在,即可删除成功成功[file removeItemAtPath:DEL_PATH
error:&error];if(error){ NSLog(@"error=%@",error);}else{ NSLog(@"删除成功");}
//test
?1234567891011121314151617181920NSFileManager *file = [NSFileManager
defaultManager]; NSArray *array = [file contentsOfDirectoryAtPath:PATH
error:nil];NSLog(@"array = %@",array); for (NSString *obj in
array) { //if([obj
hasSuffix:@"png"]) //[obj
pathExtension]: 求文件扩展名 if([[obj pathExtension]
isEqualToString:@"png"]) { //stringByAppendingPathComponent:字符串文件路径的拼接 NSString
*path = [PATH
stringByAppendingPathComponent:obj]; if([file
removeItemAtPath:path error:nil]) { NSLog(@"%@删除成功",obj); } }}
文件的读写
?123456789//NSFileHandle
文件句柄类,用于操作文件内容(读/写) //fileHandleForReadingAtPath:
以只读方式获取句柄对象//fileHandleForWritingAtPath:
以只写方式获取句柄对象//fileHandleForUpdatingAtPath:
以读写方式获取句柄对象(推荐)//获取文件句柄的对象NSFileHandle *handle = [NSFileHandle
fileHandleForUpdatingAtPath:FILE_P]; if(handle){
//如果获取句柄对象有值,则可进行读写文件
//-------读--------
?12345678910111213141516171819202122//读取指定长度的内容,存储到NSData:
readDataOfLength //默认情况下,读取文件的指定位置从起始位置开始 //设置偏移位置; 默认位置为0[handle
seekToFileOffset:3]; //偏移到文件末尾//[handle seekToEndOfFile]; //NSData:
二进制数据流类(非文本文件必须用NSData,如视图流。。)NSData *data = [handle
readDataOfLength:5]; //从指定位置读取文件内容,一直到末尾:readDataToEndOfFileNSData
*data2 = [handle readDataToEndOfFile]; //NSData转NSStringNSString *str =
[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];NSLog(@"str =
%@",str); NSString *str2 = [[NSString alloc] initWithData:data2
encoding:NSUTF8StringEncoding];NSLog(@"str2 = %@",str2);
//————–写————-
?12345678910 NSString *str3 =
@"hello_1605"; //NSString转NSData NSData
*data3 = [str3
dataUsingEncoding:NSUTF8StringEncoding]; //将游标偏移位置起始位置 [handle
seekToFileOffset:0]; [handle
writeData:data3]; //将数据流写入文件中}
//文本文件的读写操作
?123456789101112131415//参数1:路径 参数2:编码 参数3:
错误 //---读取文件---NSString *str = [NSString stringWithContentsOfFile:PATH
encoding:NSUTF8StringEncoding error:nil]; NSLog(@"str =
%@",str); //-----写入文件------NSString *str2 = @"hello
world!"; //字符串写入后,替换原先文件中的内容if([str2 writeToFile:PATH
atomically:YES]){ NSLog(@"文件写入成功");}
test
?1NSFileManager *file = [NSFileManager defaultManager];
//1. 创建目录
?12345678910111213if([file
fileExistsAtPath:DIR_PATH]){ NSLog(@"该目录已存在");}else{ BOOL
bval =[file createDirectoryAtPath:DIR_PATH withIntermediateDirectories:YES
attributes:nil
error:nil]; if(bval){ NSLog(@"目录创建成功"); }else{ NSLog(@"目录创建失败"); return
0; }}
//2. 将字符串写入到目录所在的文件中
?12345678NSString *str = @"I am a handsome man"; if([str
writeToFile:FILE_PATH
atomically:YES]){ NSLog(@"文件写入成功");}else{ NSLog(@"文件创建失败");}
//3. 读取文件中的内容
?12345678NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:FILE_PATH];if(handle){ //偏移7个字符串长度 [handle
seekToFileOffset:7]; NSData *data = [handle
readDataToEndOfFile]; NSString *str = [[NSString
alloc] initWithData:data
encoding:NSUTF8StringEncoding]; NSLog(@"str =
%@",str);}
plist文件操作
?12345//plist://1. 以列表的方式存储数据的文件//2. 文件只能以数组或字典的类型进行存储//3.
用字典的方式写入,则用字典的方式读 //用数组的方式写入,则用数组的方式读
//——-plist文件的写入——-
//1. 手动写入(新建文件-resource-property List-新建plist文件)
?123456//写入时按字典类型写入,按字典类型进行接收 //---按字典方式读取----//dictionaryWithContentsOfFile:
以字典方式读取plist文件的方法NSDictionary *dic = [NSDictionary
dictionaryWithContentsOfFile:PATH];NSLog(@"dic = %@",dic);
//2. 代码写入
?12345678910NSNumber *numA = @333;NSArray *array =
@[@"111",@"222",numA]; //writeToFile:
字典数组都用该方法写入数据到plist文件if([array writeToFile:PLIST atomically:YES]){ NSLog(@"写入成功");}else{ NSLog(@"写入失败");}
//---按数组方式读取----
?123//arrayWithContentsOfFile: 以数组方式读取plist文件的方法NSArray *array2 = [NSArray
arrayWithContentsOfFile:PLIST];NSLog(@"array = %@",array2);
总结
?1234567891011121314151617181920//1.单例 //a.概念:在程序的执行过程中,只初始化一次 //b.作用:节约内存;
共享内存,方便传值//2.NSFileManage--文件管理器类--操作文件或目录 //常用方法: //a.
遍历: 深度/浅度遍历 //b. 创建文件/目录、
文件是否存在,获取属性 //c. 拷贝/移动/删除文件或目录//3.NSfileHandle
--文件句柄类--操作文件内容 //a.主要操作文件的读和写 //b.读写过程如何偏移 //c.NSData类与NSString间的转换//4.
文本文件的读写操作 //a.直接通过字符串进行读写 //b.读取时只能读整体数据,写入时会覆盖文件的原始内容//4.plist//a.
以列表的方式存储数据的文件//b. 文件只能以数组或字典的类型进行存储//c.
用字典的方式写入,则用字典的方式读 //用数组的方式写入,则用数组的方式读
如果这篇文章对你有帮助,记得点赞哦~O(∩_∩)O哈!
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号