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. BestCoder Round #92

    这里是逢比赛必挂的智障选手ysf…… 不知道是因为自己菜还是心态不好……也许是后者吧,毕竟每次打比赛的时候都会很着急.lrd说我打比赛的功利性太强,想想确实是这样. 昨天打完之后自觉身败名裂没敢写出来 ...

  2. servlet中cookie和session操作

    1.1 软件中的会话 一次会话: 打开浏览器 -> 访问一些服务器内容 -> 关闭浏览器 登录场景: 打开浏览器 -> 浏览到登陆页面 -> 输入用户名和密码 -> 访问 ...

  3. KMP算法的一个简单实现

    今天学习KMP算法,参考网上内容,实现算法,摘录网页内容并记录自己的实现如下: 原文出处: http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93M ...

  4. 电脑护眼小软件f.lux

    f.lux这软件用了能不能保护好视力不好说,反正我是用了以后这么多年一直都在用,狠不下心删去.至少安装后能让心里多一些安全感! 以前老控制不住长期坐在电脑前不动,太需要有这类软件来养护.用了没太明显的 ...

  5. keras 联合训练

    转自: https://blog.csdn.net/Yan_Joy/article/details/62235704

  6. request.getRequestDispatcher().forward(request.response)

    request.getRequestDispatcher().forward(request.response)中的那两个参数是哪里来的? 2010-11-09 23:13 QQ357169111 | ...

  7. angular2 遗留问题

    1.angular build [2017-07-26]  a.改写js/css的引用目录的前缀(比如统一增加 /abc/xxx/*.js)  b.build时,可以控制index/js/css的生成 ...

  8. PHP实现无限分类

    PHP实现无限分类 无限分类 递归 无限级分类是一种设计技巧,在开发中经常使用,例如:网站目录.部门结构.文章分类.笔者觉得它在对于设计表的层级结构上面发挥很大的作用,比如大家在一些平台上面,填写邀请 ...

  9. PhoneGap 的消息推送插件JPush极光推送

    一. 什么是极光推送 极光推送,使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动, 从而有效地提高留存率,提升用户体验.平台提供整合了 Android 推送.iOS 推送的统一推 ...

  10. 哈哈,原来IOC容器的bean是存在DefaultSingletonBeanRegistry的一个Map类型的属性当中。

    经过查看源代码发现IOC容器中的bean实例(不知道是不是所有的bean)是存储在一个DefaultSingletonBeanRegistry类实例的一个Map类型的属性当中. 下面是DefaultS ...