OC-文件操作
一.归档NSKeyedArchiver==========================
1.第一种方式:存储一种数据。=====================
//归档
//第一种写法
//对象--文件
NSArray* array = [[NSArray alloc]initWithObjects:@"zhang",@"wang",@"li", nil];
NSString* filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"array.txt"];
BOOL success = [NSKeyedArchiver archiveRootObject:array toFile:filePath];
if (success) {
NSLog(@"保存成功");
}
//解归档
NSArray* arr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",arr);
2.第二种方式:存储并行数据(存储多种数据)。====================================
//第二种写法:
NSArray* array = @[@"one",@"two",@"three"];
NSDictionary* dic = @{@"key":@"value"};
NSString* str = @"我是中国人,我爱中国";
//NSData 数据流类
//用来存储复杂的数据,把复杂的数据转成数据流格式,可以方便进行存储和传输。
//例如:图片、大文件
//断点续传,假如图片有20M大,
//发邮件:添加附件
NSMutableData* data = [[NSMutableData alloc]init];
//initForWritingWithMutableData 指定要写入的数据流文件
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//编码数据,参数一:要准备编码的数据;参数二:编码数据的key,key随便写
[archiver encodeObject:array forKey:@"array"];
[archiver encodeObject:dic forKey:@"dic"];
[archiver encodeObject:str forKey:@"str"];
//编码完成
[archiver finishEncoding];
//指定文件路径
NSString* filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"file2"];
//写入文件
[data writeToFile:filePath atomically:YES];
//====================************************************=========================//
//先把路径下的文件读入数据流中
NSMutableData* fileData = [[NSMutableData alloc]initWithContentsOfFile:filePath];
//把数据流文件读入到了 解归档中
NSKeyedUnarchiver* unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:fileData];
//进行解归档
NSArray* UnArray = [unArchiver decodeObjectForKey:@"array"];
NSDictionary* UnDic = [unArchiver decodeObjectForKey:@"dic"];
NSString* UnStr = [unArchiver decodeObjectForKey:@"str"];
//打印
NSLog(@"%@\n%@\n%@\n",UnArray,UnDic,UnStr);
三.第三种归档方式:对类对象进行归档=======================================
1.先在类的头文件中实现<NSCoding>协议
2.在.m中重新编码和解码协议。
//重新initWithCoder 解码方法
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"我是解码方法,我负责解码");
self = [super init];
if (self) {
_name = [aDecoder decodeObjectForKey:@"name"];
_phone = [aDecoder decodeObjectForKey:@"phone"];
_address = [aDecoder decodeObjectForKey:@"address"];
}
return self;
}
//重新编码方法
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_phone forKey:@"phone"];
[aCoder encodeObject:_address forKey:@"address"];
}
//【注】forKey:是字符串;编码方法和解码方法字符串要一致
四.NSFileManager 文件管理类===============================================
1.文件路径------------------------------------------------------------------------------
//根路径
NSString* homePath = NSHomeDirectory();
NSLog(@"%@",homePath);
//oc中有三个目录是可以操作的。
/*
1.Documents//文稿目录
2.tmp//临时目录:程序退出的时候,临时目录内容可能会被情况
3.library--->Cache//缓存目录//app目录下
*/
//获取Documents 目录
//写法一
NSString* Documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"Documents :%@",Documents);
//写法二
NSString* Documents1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES) objectAtIndex:0];
NSLog(@"Documents : %@",Documents1);
//获取应用程序的主目录
NSString* userName = NSUserName();
NSString* rootPath = NSHomeDirectoryForUser(userName);
NSLog(@"app root path:%@",rootPath);
//获取cache目录
NSString* cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"cache :%@",cachePath);
//获取tmp目录
NSString* tmpPath = NSTemporaryDirectory();
NSLog(@"tmp:%@",tmpPath);
2.创建目录和文件------------------------------------------------------------------------------------
//获取根目录
NSString* homePath = NSHomeDirectory();
//创建了一个文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
//拼接出想要创建的文件路径
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder",homePath];
//创建文件目录
//第一个参数传入想要创建的文件目录,第二个参数指导是否创建不存在的文件夹,yes代表创建
BOOL isOk = [fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
if (isOk) {
NSLog(@"创建文件目录成功");
}
NSString* string = @"我爱记歌词";
//把内容写入到指定路径下的指定文件中
BOOL isWriteOk = [string writeToFile:[NSString stringWithFormat:@"%@/1.txt",filePath] atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (isWriteOk) {
NSLog(@"写入文件成功");
}
//数组保存
NSArray* array = @[@"我是一",@"我是三",@"我是周7"];
BOOL isWriteOK1 = [array writeToFile:[NSString stringWithFormat:@"%@/2.txt",filePath] atomically:YES];
if (isWriteOK1) {
NSLog(@"数组写入文件成功");
}
//字典保存
NSDictionary* dic = @{@"key":@"value"};
BOOL isWriteOK2 = [dic writeToFile:[NSString stringWithFormat:@"%@/3.txt",filePath] atomically:YES];
if (isWriteOK2) {
NSLog(@"字典写入文件成功");
}
3.对文件进行重命名---------------------------------------------------------------------------------------------
//获取根目录
NSString* homePath = NSHomeDirectory();
//创建了一个文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
//拼接出想要创建的文件路径
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder/1.txt",homePath];
[fileMgr moveItemAtPath:filePath toPath:[NSString stringWithFormat:@"%@/ai.txt",homePath] error:nil];
4.删除一个文件---------------------------------------------------------------------------------------------
//声明了一个错误信息的对象
NSError* error;
//获取根目录
NSString* homePath = NSHomeDirectory();
//创建了一个文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
//拼接出想要创建的文件路径
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder/3.txt",homePath];
//删除文件
//如果方法执行返回是NO,error会保存错误信息,如果方法执行返回是YES,error = nil
BOOL isok = [fileMgr removeItemAtPath:filePath error:&error];
if (isok) {
NSLog(@"删除文件成功");
}
else
{
NSLog(@"删除文件失败");
//打印错误信息
NSLog(@"%@",error.localizedDescription);
}
Δ【扩展】NSError类,是一个错误信息类
//删除文件
//如果方法执行返回是NO,error会保存错误信息,如果方法执行返回是YES,error = nil
BOOL isok = [fileMgr removeItemAtPath:filePath error:&error];
∆5.删除目录下的所有文件---------------------------------------------------------------------------------------------
//获取根目录
NSString* homePath = NSHomeDirectory();
//创建了一个文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
//拼接出想要创建的文件路径
// NSString* filePath = [NSString stringWithFormat:@"%@/myFolder/3.txt",homePath];
//如果删除的目录中不带具体的文件,则删除的是整个目录
[fileMgr removeItemAtPath:[NSString stringWithFormat:@"%@/myFolder/",homePath] error:nil];
6.获取目录下的所有文件---------------------------------------------------------------------------------------------
//获取根目录
NSString* homePath = NSHomeDirectory();
//创建了一个文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
//拼接出想要创建的文件路径
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder/",homePath];
//获取当前目录下的所有文件,包括隐藏文件
NSArray* allFile = [fileMgr contentsOfDirectoryAtPath:filePath error:nil];
NSLog(@"%@",allFile);
//获取当前目录以及子目录的所有文件
NSArray* subAllFile = [fileMgr subpathsOfDirectoryAtPath:filePath error:nil];
7.文件的属性---------------------------------------------------------------------------------------------
//获取根目录
NSString* homePath = NSHomeDirectory();
//创建了一个文件管理器
NSFileManager* fileMgr = [NSFileManager defaultManager];
//拼接出想要创建的文件路径
NSString* filePath = [NSString stringWithFormat:@"%@/myFolder/3.txt",homePath];
NSDictionary* fileAttribute = [fileMgr fileAttributesAtPath:filePath traverseLink:YES];
//获取文件的属性
NSData* data = [fileAttribute objectForKey:NSFileModificationDate];
NSLog(@"文件的创建日期:%@",data);
//文件占多少字节
NSNumber * number = [fileAttribute objectForKey:NSFileSize];
NSLog(@"文件的大小:%@",number);
OC-文件操作的更多相关文章
- OC文件操作1
主要内容: 1)文件操作:对文件本身的操作(NSManager) 2)对文件内容的操作(NSHandle) 1.NSManager 创建一个单例的file manager的对象 //创建一个单例的fi ...
- OC文件操作(2)
NSFileManager 文件管理器完成文件的创建.移动.拷贝等管理操作 1.查询文件和目录 OC中查询路径下的目录主要分为浅度遍历和深度遍历. 浅度遍历 NSFileManager * ma ...
- OC文件操作(1)
1.文件的浅度遍历与深度遍历: //NSFileManager * fm = [[NSFileManager alloc]init];//创建文件管理器 //第一步创建一个文件管理器 NSError ...
- OC文件操作2
1.对文件本身的操作 NSManager 2.对文件内容的操作 NSHandle 文件句柄 NSFileHandle * fh = [NSFileHandle fileHandleForReading ...
- OC文件操作、获取文件属性
#import <Foundation/Foundation.h> //获取文件的属性 int main(int argc, const char * argv[]) { @autorel ...
- 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)
========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...
- PHP文件操作系统----主要的文件操作函数
一.文件操作系统概述 1.概述: php中的文件操作系统主要是对文件和目录的操作.文件在windows系统下分为3种不同:文件.目录.未知,在linux/unix系统下分为7种不同:block.cha ...
- 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档
.net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...
- 野路子出身PowerShell 文件操作实用功能
本文出处:http://www.cnblogs.com/wy123/p/6129498.html 因工作需要,处理一批文件,本想写C#来处理的,后来想想这个是PowerShell的天职,索性就网上各种 ...
- Node基础篇(文件操作)
文件操作 相关模块 Node内核提供了很多与文件操作相关的模块,每个模块都提供了一些最基本的操作API,在NPM中也有社区提供的功能包 fs: 基础的文件操作 API path: 提供和路径相关的操作 ...
随机推荐
- Android图片加载框架Picasso最全使用教程1
Picasso介绍 Picasso是Square公司开源的一个Android图形缓存库 A powerful image downloading and caching library for And ...
- HDU4223:Dynamic Programming?(简单dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4223 求连续子序列和的最小绝对值,水题. #include <iostream> #inclu ...
- java多线程总结(一)
在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 对于直接继承Thread的类来说,代码大致框架是: 1 2 3 4 5 6 7 8 9 10 11 ...
- Excel常见操作,重复数据,去除数据关联
Eecel对一个数据进行操作后按住右下角的十字架往下拉就可以对下面的操作进行相同 的操作,所以只需先对一个数据进行操作,再拉下来就可以了 通过公式处理的数据跟其它数据有关联 需要对这些数据进行去除它们 ...
- rails timeout 异常
发现经常有”超时“的错误信息,如/usr/lib/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired (Timeout::Error),恩 ...
- CNN学习笔记:卷积神经网络
CNN学习笔记:卷积神经网络 卷积神经网络 基本结构 卷积神经网络是一种层次模型,其输入是原始数据,如RGB图像.音频等.卷积神经网络通过卷积(convolution)操作.汇合(pooling)操作 ...
- Linux 初始化之 Systemd机制
systemd是Linux下的一种init软件,由Lennart Poettering带头开发,其开发目标是提供更优秀的框架以表示系统服务间的依赖关系,并依此实现系统初始化时服务的并行启动,同时达到降 ...
- [转]hadoop2.x常用端口及定义方法
端口 Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以及HTTP访问.而随着Hadoop周边组件的增多,完全记不住哪个端口对应哪个应用,特收集 ...
- 响应式Tab选项卡
在线演示 本地下载
- Ubuntu gcc错误:对'log'等函数未定义的引用
Ubuntu gcc错误:对'log'等函数未定义的引用 a.c #include <stdio.h>#include <math.h>int main(){ float ...