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更)的更多相关文章

  1. IOS文件操作的两种方式:NSFileManager操作和流操作

    1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...

  2. iOS——文件操作NSFileManager (创建、删除,复制,粘贴)

    iOS——文件操作NSFileManager (创建.删除,复制,粘贴)       iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...

  3. iOS文件路径相关的方法

    文件路径相关的方法在NSPathUtilities中,主要是操作路径 获得一个路径 NSString *documents = [NSSearchPathForDirectoriesInDomains ...

  4. ios 文件操作

    1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...

  5. ios 文件操作(NSFileManager)

    IOS的沙盒机制,应用只能访问自己应用目录下的文件,iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内. ...

  6. iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)

    版权声明:本文为博主原创文章,转载请声明出处:http://blog.csdn.net/jinnchang 1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系.每个 iOS 应用程 ...

  7. Swift iOS 文件操作:沙盒(SandBox)、程序包(NSBundle)

    1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系.每个 iOS 应用程序都有一个单独的文件系统(存储空间),而且只能在对应的文件系统中进行操作,此区域被称为沙盒.所有的非代码文件 ...

  8. 【精】iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)

    1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系. 每一个 iOS 应用程序都有一个单独的文件系统(存储空间).并且仅仅能在相应的文件系统中进行操作,此区域被称为沙盒. 全部的非 ...

  9. Linux(三) - 文件操作相关命令

    Ctl-A 光标移动到行首 Ctl-C 终止命令 Ctl-D 注销登录 Ctl-E 光标移动到行尾 Ctl-U 删除光标到行首的所有字符,在某些设置下,删除全行 Ctl-W 删除当前光标到前边的最近一 ...

随机推荐

  1. [摘]全文检索引擎Solr系列—–全文检索基本原理

    原文链接--http://www.importnew.com/12707.html 全文检索引擎Solr系列—–全文检索基本原理 2014/08/18 | 分类: 基础技术, 教程 | 2 条评论 | ...

  2. 视差滚动-background-attachement

    之前项目中没有涉及到视觉滚动的网站,但是毕竟是一种常用的网站类别,不得不了解.实现方法很简单,做一下简单的分析... 概述:滚动视差是指多层背景以不同的速度移动,形成立体的运动效果,来带非常出色的视觉 ...

  3. angularJS articles and resources

    http://antjanus.com/blog/web-development-tutorials/front-end-development/comprehensive-beginner-guid ...

  4. Linux ->> Apt-get命令安装软件

    Apt全称Advanced Package Tool.Apt-get适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索.安装.升级.卸载软件或操作系统. 用法: apt-cache ...

  5. Linux ->> uname命令 -- 查看当前系统信息

    uname命令是用来打印当前Linux操作系统的信息,如果操作系统内核版本,操作系统是32位还是64位,计算机名字,操作系统安装时间,Linux操作系统类型 1) 操作系统是32位还是64位,感觉-m ...

  6. C++ virtual inheritance ZZ

       虚继承 是面向对象编程中的一种技术,是指一个指定的基类,在继承体系结构中,将其成员数据实例共享给也从这个基类型直接或间接派生的其它类. 举例来说:假如类A和类B各自从类X派生(非虚继承且假设类X ...

  7. jbd2/dm-2-8 io太高

    用iotop查看发现[jbd2/dm-2-8]几乎占用了99%的io使用率,但是却没有输入输出 后来上网查找,网上有人说是个内核bug,需要升级内核,或者降低jdb2的提交次数,即重新挂载磁盘 添加c ...

  8. 学习Road map Part 04 自动驾驶、SLAM、ROS、树莓派

    学习Road map Part 04 自动驾驶.SLAM.ROS.树莓派

  9. MVC 接收参数时会自动解码

    MVC在接收传递过来的参数时,会自动对参数进行解码,无需手动解码 例: public ActionResult SendMsg2(string name) { return Content(name) ...

  10. 条件独立(conditional independence) 结合贝叶斯网络(Bayesian network) 概率有向图 (PRML8.2总结)

    转:http://www.cnblogs.com/Dzhouqi/p/3204481.html本文会利用到上篇,博客的分解定理,需要的可以查找上篇博客 D-separation对任何用有向图表示的概率 ...