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 删除当前光标到前边的最近一 ...
随机推荐
- web前端优化之内容优化
前端内容优化主要有以下几条: 1.尽量减少http请求 (1)合并文件,把多个css文件合并在一起: (2)css Sprites,把css相关的background元素进行背景图绝对定位: (3)图 ...
- 【免费公测】阿里云SSD云盘,不仅仅是IO提速10倍
今天很高兴为大家介绍最新的ECS存储服务:SSD云盘. SSD云盘基于全SSD存储介质.利用阿里云飞天分布式存储技术,提供数据可靠性99.999%的高性能存储:该产品具备以下特点: l 高性能:单个 ...
- 【Python】猜数小游戏(文件操作)
人生苦短,我用Python 关键词 1.多用户 2.字典记录所有成绩 3.每次游戏轮数&总游戏次数&平均每次游戏需要多少轮 字典Dictionary.列表List.元组Tuple差异化 ...
- 什么是shell? bash和shell有什么关系?
什么是Shell? shell是你(用户)和Linux(或者更准确的说,是你和Linux内核)之间的接口程序.你在提示符下输入的每个命令都由shell先解释然后传给Linux内核. ...
- 小Y的轮回之路——攒机装机、B150装win7
两个月前,陪伴我5年多的小Y(ideapad-y460N卡)突然大伤元气,硬盘跪了,显示屏也黑了一小块.本着经济实惠凑合用的态度换了个320G的硬盘,没想过几天显示屏情况加重,出现无数个红绿相间的线条 ...
- 遍历查询结果集,update数据
select NULL mykey, * into #mytemp from dbo.DIM_DISTRIBUTOR declare @i int begin ) print @i )) where ...
- MVC的验证(模型注解和非侵入式脚本的结合使用)
@HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客户端,后台服务器的验证,MVC统统都做了包含,即使用户 ...
- [python错误]UnicodeDecodeError: 'gbk' codec can't decode byte...
出现此错误的原因是使用'gbk'解码时报错,存在一些字符不能使用gbk来解码. 首先,简体中文字符编码(ASCII扩展字符集)有下列几种:GB2312.GBK.GB18030. GB2312: 中国国 ...
- python UI自动化实战记录八:添加配置
添加配置文件写入测试地址等,当环境切换时只需修改配置文件即可. 1 在项目目录下添加文件 config.ini 写入: [Domain] domain = http://test.domain.cn ...
- libevent核心-event和event_base结构体
参考:http://blog.csdn.net/yusiguyuan/article/category/2171081/2 http://blog.csdn.net/sparkliang/articl ...