iOS:文件操作相关(18-03-23更)
0、iOS文件系统
1、工程内文件
2、文件夹管理
3、文件操作
4、NSCache
附录:
1、沙盒文件夹、文件大小
2、清除沙盒 Library / Cache 下所有数据
3、测试plist
0、写在前面
1、文件名不能有“/”,“/”代表下一级目录。如要做文件缓存:
1)、要么,把文件名(网络地址),“/”替换成如“_”,[fileUrl stringByReplacingOccurrencesOfString:@"/" withString:@"_"] 。
2)、要么,MD5编码,换新名。
2、contentsOfDirectoryAtPath:只获取该目录下的 根文件夹、根文件。
subpathsAtPath:遍历出该目录下所有的 子文件夹、子文件。
==================== iOS文件系统 ====================
Document:一般需要持久的数据都放在此目录中。用户操作生成的数据。(iTunes备份和恢复)
Library:设置程序的默认设置和其他状态信息。网络下载的数据。(iTunes备份和恢复,除了 Library / Cache )
Library / Preferences : 偏好设置(最好不要动,NSUserDefaults 的 plist 文件保存在这个目录下,只能存一份,会覆盖)
Library / Cache : 缓存,不会备份
tmp:创建临时文件的目录,当iOS设备重启时,文件会被自动清除
补充:数据只能保存在上面3个文件夹里,否则无法写入,同时,乱写入到不对应的文件夹,也有可能上架不了。
0、直接获取
// 获取 根目录 路径
NSString *userRootPath = NSHomeDirectory();
// 获取 tmp 路径
NSString *userTmpPath = NSTemporaryDirectory();
1、拼接写法
#define kDocumentsPath [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
#define kLibraryPath [NSHomeDirectory() stringByAppendingPathComponent:@"Library"]
#define kLibrary_Caches_Path [kLibraryPath stringByAppendingPathComponent:@"Caches"]
补充:字符串形式,容易写错、或者修改到。
2、搜索写法
// 参数1:
// NSDocumentDirectory : 获取 Documents 路径
// NSLibraryDirectory : 获取 Library 路径
// NSCachesDirectory : 获取 Library / cache 路径
// 参数:
// NSUserDomainMask : 只在 user 里搜索
// 参数3:
// YES : YES,根目录显示出来,NO,根目录为"~"
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
补充:因为是搜索 文件夹/文件 名,所以多个重名的时候,可能有问题( firstObject 好 还是 lastObject 好?)?具体没试过。
==================== 工程内文件 ====================
0、工程路径
[[NSBundle mainBundle] bundlePath];
1、获取文件路径
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];
NSString *voicePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"wav"];
2、获取文件
UIImage*appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
音频参照“iOS:小技巧” -> “7、播放音频” -> “2)工程内音频”
==================== 文件夹管理 ====================
0、写在前面。创建、或删除等等操作,看需求先判断是否存在,再操作。
//判断文件夹、文件是否存在
[fileManager fileExistsAtPath:oldPath]
1、拿到文件管理者单例
NSFileManager *fileManager = [NSFileManager defaultManager];
2、使用管理者创建文件夹
//path:要创建的文件夹名,文件夹名是没有后缀的
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]
3、创建文件(plist文件,直接用数组、字典自带的write方法)
//filePath:在之前文件夹下创建的文件,为“xxx.xxx” data要编码
[fileManager createFileAtPath:filePath contents:data attributes:nil]
4、读取文件信息
//返回字典
[fileManager attributesOfItemAtPath:filePath error:&error]
5、读取文件返回的字典信息
[infoDic objectForKey:@"NSFileSize"]
6、文件读取
6-1)、方法1:
//读到NSData
NSData *newData = [fileManager contentsAtPath:filePath];
//解码
[[NSString alloc]initWithData:newData encoding:NSUTF8StringEncoding];
6-2)、方法2:
[[NSString alloc]initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
7、文件移动(剪切、重命名)
//文件名有后缀”xxx.xx”
[fileManager moveItemAtPath:oldPath toPath:newPath error:&error]
8、文件复制
//文件名有后缀“xxx.xx”
[fileManager copyItemAtPath:oldPath toPath:newPath error:&error]
9、文件删除
//文件名有后缀“xxx.xx”
[fileManager removeItemAtPath:oldPath error:&error]
==================== 文件操作 ====================
1、写入
1-1)、设置为写入模式
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
1-2)、先转码
NSString *newString = @"hello,hello,hello,世界";
NSData *newData = [new_string dataUsingEncoding:NSUTF8StringEncoding];
1-3)、再写入
[fileHandle writeData:newData];
1-4)、追加
//先找到最后的
[fileHandle seekToEndOfFile]; //写入转码后的数据
[fileHandle writeData:newData2];
1-5)、覆盖(如需插入操作,1、全读取,2、插入,3、全写入)
//先找到偏移位
[fileHandle seekToFileOffset:3]; //写入转码后的数据
[fileHandle writeData:newData3];
1-6)、关闭操作
[fileHandle closeFile];
2、读取
2-1)、设置为读取模式
fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
2-2)、读取数据
//读取所有data
NSData *readData = [fileHandle readDataToEndOfFile]; //转成字符串
NSString *readString = [[NSString alloc]initWithData:readData encoding:NSUTF8StringEncoding];
2-3)、再次读取数据
//设置为读取模式,否则有问题
fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
2-4)、读取前几个数据
//读取
readData = [fileHandle readDataOfLength:10]; //转成字符串
readString = [[NSString alloc]initWithData:readData encoding:NSUTF8StringEncoding];
2-5)、关闭操作
[fileHandle closeFile];
==================== NSCache ====================
1、属性
// 自定义名字,以做区分
name;
// 最多存放 数据大小 eg.10*1000(好像在哪看到,苹果的GB、MB、KB 是1000 ,不是1024,不一定正确)
totalCostLimit;
// 最多存放 个数 eg.5
countLimit;
// 移除 废弃内容 的对象
evictsObjectsWithDiscardedContent;
2、方法
// 按 key 取对象
- (nullable ObjectType)objectForKey:(KeyType)key;
// 按 key 设置新的对象
- (void)setObject:(ObjectType)obj forKey:(KeyType)key;
// 按 key 设置新的对象 cost:指定花费内存大小?
- (void)setObject:(ObjectType)obj forKey:(KeyType)key cost:(NSUInteger)g;
// 按 key 移除对象
- (void)removeObjectForKey:(KeyType)key;
// 移除所有对象
- (void)removeAllObjects;
3、代理
// 代理
delegate;
// 签代理,可选项
// 对象将要移除,可根据需要保存在手机上。
- (void)cache:(NSCache *)cache willEvictObject:(id)obj;
附录:
==================== 沙盒文件夹、文件大小 ====================
1、单个文件大小
- (unsigned long long)fileSizeAtPath:(NSString*)filePath
{
unsigned long long fileSize = 0; NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
fileSize = [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return fileSize;
}
2、整个文件夹下的大小,以M为单位
- (float)folderSizeAtPath:(NSString*)folderPath
{
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:folderPath] == NO) {
return 0;
} NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
NSString *fileName;
unsigned long long folderSize = 0;
while ((fileName = [childFilesEnumerator nextObject]) != nil){
NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
return folderSize/(1000.0*1000.0);
}
3、判断是M还是G,并以字符显示
- (NSString*)getCachesString
{
CGFloat size = [self folderSizeAtPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) firstObject]]; if (size >= 1000.0 ){ // 大于1G
return [NSString stringWithFormat:@"%.1fG",size/1000.0];
}else{ // 最小以M为单位
return [NSString stringWithFormat:@"%.1fM",size];
}
}
==================== 清除沙盒 Library / Cache 下所有数据 ====================
1、清除文件夹
- (void)clearFileAtPath:(NSString*)filePath{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *filePathsArray = [fileManager contentsOfDirectoryAtPath:filePath error:nil];
for (NSString *fileName in filePathsArray) {
[fileManager removeItemAtPath:[filePath stringByAppendingPathComponent:fileName]
error:nil];
}
}
2、清除沙盒 Library / Cache
- (void)clearLibCashesAllFile{
NSString *cachesFilePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) firstObject];
[self clearFileAtPath:cachesFilePath];
}
==================== 测试plist ====================
1、获取应用名称
// 获取应用名称
- (NSString*)getAppName
{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *appName = [bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
if (!appName) {
appName = [bundle objectForInfoDictionaryKey:@"CFBundleName"];
}
return appName;
}
2、获取版本号
// 版本号
- (NSString*)getVersion
{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *version = [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
return version;
}
3、指向桌面测试plist文件的路径,没有的话再创建
- (NSString*)testPlistName:(NSString*)plistName
{
NSString *bundle = [[NSBundle mainBundle] resourcePath];
NSString *bundle_desktop = [[bundle substringToIndex:[bundle rangeOfString:@"Library"].location] stringByAppendingFormat:@"Desktop"];
NSString *bundle_desktop_appName = [bundle_desktop stringByAppendingPathComponent:[self getAppName]];
NSString *bundle_desktop_appName_version = [bundle_desktop_appName stringByAppendingPathComponent:[NSString stringWithFormat:@"version %@",[self getVersion]]]; NSString *plistPath = [NSString stringWithFormat:@"%@.plist",plistName];
NSString *location = [bundle_desktop_appName_version stringByAppendingPathComponent:plistPath]; NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:bundle_desktop_appName] == NO) {
[[NSFileManager defaultManager] createDirectoryAtPath:bundle_desktop_appName withIntermediateDirectories:YES attributes:nil error:&error];
} if ([[NSFileManager defaultManager] fileExistsAtPath:bundle_desktop_appName_version] == NO) {
[[NSFileManager defaultManager] createDirectoryAtPath:bundle_desktop_appName_version withIntermediateDirectories:YES attributes:nil error:&error];
}
return location;
}
iOS:文件操作相关(18-03-23更)的更多相关文章
- IOS文件操作的两种方式:NSFileManager操作和流操作
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- iOS——文件操作NSFileManager (创建、删除,复制,粘贴)
iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...
- iOS文件路径相关的方法
文件路径相关的方法在NSPathUtilities中,主要是操作路径 获得一个路径 NSString *documents = [NSSearchPathForDirectoriesInDomains ...
- ios 文件操作
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- ios 文件操作(NSFileManager)
IOS的沙盒机制,应用只能访问自己应用目录下的文件,iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内. ...
- iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)
版权声明:本文为博主原创文章,转载请声明出处:http://blog.csdn.net/jinnchang 1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系.每个 iOS 应用程 ...
- Swift iOS 文件操作:沙盒(SandBox)、程序包(NSBundle)
1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系.每个 iOS 应用程序都有一个单独的文件系统(存储空间),而且只能在对应的文件系统中进行操作,此区域被称为沙盒.所有的非代码文件 ...
- 【精】iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)
1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系. 每一个 iOS 应用程序都有一个单独的文件系统(存储空间).并且仅仅能在相应的文件系统中进行操作,此区域被称为沙盒. 全部的非 ...
- Linux(三) - 文件操作相关命令
Ctl-A 光标移动到行首 Ctl-C 终止命令 Ctl-D 注销登录 Ctl-E 光标移动到行尾 Ctl-U 删除光标到行首的所有字符,在某些设置下,删除全行 Ctl-W 删除当前光标到前边的最近一 ...
随机推荐
- Postman安装及入门实践(以百度搜索为例)
一.Postman安装 可以FQ的小伙伴可以直接去官网下载:https://www.getpostman.com 如果不能,可以用我的安装包,版本找最新的:链接:https://pan.baidu.c ...
- 【免费公测】阿里云SSD云盘,不仅仅是IO提速10倍
今天很高兴为大家介绍最新的ECS存储服务:SSD云盘. SSD云盘基于全SSD存储介质.利用阿里云飞天分布式存储技术,提供数据可靠性99.999%的高性能存储:该产品具备以下特点: l 高性能:单个 ...
- CSS布局之传统方法
传统的页面布局依赖于盒模型+流动模型(flow)+浮动模型(float)+层模型(layer)来实现页面的布局,具体方法是通过盒模型+display属性+float属性+position属性来加以实现 ...
- 阿里云上面部署mysql
0.卸载已有的mysql 系统中可能早已存在mysql数据库,所以在安装之前我们需要将其卸载掉. # rpm -qa|grep -i mysql 该命令将显示已经安装了的mysql软件,之后通过下面的 ...
- Incorrect string value: '\xE8\xAF\xAD\xE6\x96\x87' for column 'name' at row 1
报错的原因就是在执行插入时对Name这个字段被赋予了错误的字符串值:’\xE4\xB8\xAD\xE6\x96\x87’ 实际上就函数里面的变量接收到的值编码格式跟它定义的不一致. 使用navicat ...
- Netty相关面试题
1.BIO.NIO和AIO的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要启动一个线程进行处理.线程开销大. 伪异步IO:将请求连接放入线程池,一对多,但线程还是很宝贵的资源. N ...
- Linux下配置免密登录!
ssh-keygen 产生公钥与私钥对. ssh-copy-id 将本机的公钥复制到远程机器的authorized_keys文件中,ssh-copy-id也能让你有到远程机器的home, ~./ss ...
- linux中无法使用sudo的方法
xxx is not in the sudoers file.This incident will be reported.的解决方法 1.切换到root用户下. 2.添加sudo文件的写权限,命令是 ...
- Redis 4.0+安装及配置
系统环境:CentOS 7.3 官方下载最新版:https://redis.io/download:或直接终端下载解析安装: $ wget http://download.redis.io/relea ...
- Hibernate、Mybatis与Spring Data JPA
从零开始集成Springboot+MyBatis+JPA https://www.jianshu.com/p/e14c4a6f6871 MyBatis 与Hibernate的区别 http://xhr ...