iOS开发-文件管理之多的是你不知道的事(一)
郝萌主倾心贡献。尊重作者的劳动成果,请勿转载。
假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^
我要捐赠: 点击捐赠
Cocos2d-X源代码下载:点我传送
游戏官方下载:
http://dwz.cn/RwTjl
游戏视频预览:
http://dwz.cn/RzHHd
游戏开发博客:
http://dwz.cn/RzJzI
游戏源代码传送:
http://dwz.cn/Nret1
一、iOS中的沙盒机制
iOS应用程序仅仅能对自己创建的文件系统读取文件,这个独立、封闭、安全的空间,叫做沙盒。
它一般存放着程序包文件(可运行文件)、图片、音频、视频、plist文件、sqlite数据库以及其它文件。
每一个应用程序都有自己的独立的存储空间(沙盒)
一般来说应用程序之间是不能够互相訪问
模拟器沙盒的位置
/User/userName/Library/Application Support/iPhone Simulator
当我们创建应用程序时,在每一个沙盒中含有三个文件,各自是Document、Library和temp。
Document:一般须要持久的数据都放在此文件夹中,能够在其中加入子文件夹。iTunes备份和恢复的时候,会包含此文件夹。
Library:设置程序的默认设置和其它状态信息
temp:创建暂时文件的文件夹,当iOS设备重新启动时,文件会被自己主动清除
获取沙盒文件夹
获取程序的根文件夹(home)文件夹
NSString *homePath = NSHomeDirectory()
获取Document文件夹
NSArray *paths = NSSearchPathDorDirectoriesInDomains(NSDocumentDicrectory,NSUserDomainMark,YES);
NSString *docPath = [paths lastObject];
获取Library文件夹
NSArray *paths =
NSSearchPathForDirectoriseInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths lastObject];
获取Library中的Cache
NSArray *paths =
NSSearchPathForDirectoriseInDomains(NSCachesDirectory, NSUserDomainMask,YES);
NSString *docPath = [paths lastObject];
获取temp路径
NSString *temp = NSTemporaryDirectory( );
二、NSString类路径的处理方法
文件路径的处理
NSString *path = @"/Uesrs/apple/testfile.txt"
经常用法例如以下
获得组成此路径的各个组成部分。结果:("/","User","apple","testfile.txt")
- (NSArray *)pathComponents;
提取路径的最后一个组成部分。结果:testfile.txt
- (NSString *)lastPathComponent;
删除路径的最后一个组成部分,结果:/Users/apple
- (NSString *)stringByDeletingLastPathCpmponent;
将path加入到先邮路径的末尾,结果:/Users/apple/testfile.txt/app.txt
- (NSString *)stringByAppendingPathConmponent:(NSString *)str;
去路径最后部分的扩展名。结果:text
- (NSString *)pathExtension;
删除路径最后部分的扩展名,结果:/Users/apple/testfile
- (NSString *)stringByDeletingPathExtension;
路径最后部分追加扩展名,结果:/User/apple/testfile.txt.jpg
- (NSString *)stringByAppendingPathExtension:(NSString *)str;
三、NSData
NSData是用来包装数据的
NSData存储的是二进制数据,屏蔽了数据之间的差异。文本、音频、图像等数据都可用NSData来存储
NSData的用法
1.NSString与NSData互相转换
NSData-> NSString
NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding];
NSString->NSData
NSString *aString = @"1234abcd";
NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
将data类型的数据,转成UTF8的数据
+(NSString *)dataToUTF8String:(NSData *)data
{
NSString *buf = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return [buf autorelease];
}
将string转换为指定编码
+(NSString *)changeDataToEncodinString:(NSData *)data encodin:(NSStringEncoding )encodin{
NSString *buf = [[[NSString alloc] initWithData:data encoding:encodin] autorelease];
return buf;
}
2. NSData 与 UIImage
NSData->UIImage
UIImage *aimage = [UIImage imageWithData: imageData];
//例:从本地文件沙盒中取图片并转换为NSData
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *name = [NSString stringWithFormat:@"ceshi.png"];
NSString *finalPath = [path stringByAppendingPathComponent:name];
NSData *imageData = [NSData dataWithContentsOfFile: finalPath];
UIImage *aimage = [UIImage imageWithData: imageData];
3.NSData与NSArray NSDictionary
+(NSString *)getLocalFilePath:(NSString *) fileName
{
return [NSString stringWithFormat:@"%@/%@%@", NSHomeDirectory(),@“Documents”,fileName];
}
包含将NSData写进Documents文件夹
从Documents文件夹读取数据
在进行网络数据通信的时候。经常会遇到NSData类型的数据。
在该数据是dictionary结构的情况下,系统没有提供现成的转换成NSDictionary的方法,
为此能够通过Category对NSDictionary进行扩展。以支持从NSData到NSDictionary的转换。
声明和实现例如以下:
+ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data {
CFPropertyListRef list = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)data, kCFPropertyListImmutable, NULL);
if(list == nil) return nil;
if ([(id)list isKindOfClass:[NSDictionary class]]) {
return [(NSDictionary *)list autorelease];
}
else {
CFRelease(list);
return nil;
}
}
四、文件管理经常用法
NSFileManager
创建一个文件并写入数据
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
从一个文件里读取数据
- (NSData *)contentsAtPath:(NSString *)path;
scrPath路径上的文件移动到dstPath路径上。注意这里的路径是文件路径而不是文件夹
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **) error;
scrPath路径上的文件拷贝到dstPath路径上
- (BOOL)copyItemAtPath:(NSString *)scrPath toPath:(NSString *)dstPath error:(NSError **) error;
比較两个文件的内容是否一样
- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2;
文件时候存在
- (BOOL)fileExistsAtPath:(NSString *)path;
移除文件
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **) error;
创建文件管理
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path =
[NSHomeDirectory( ) stringByAppendingPathComponent:@"holyBible.txt"];
NSString *text = @"abcdefg";
将字符串转成NSData类型
NSData *data = [text dataUsingEncoding: NSUTF8StringEncoding];
写入文件
BOOL success = [fileManager createFileAtPath:path contents:data attributes:nil];
创建文件夹
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSString *contect = @"abcdefg";
BOOL success = [fm createFileAtPath:filePath contents:[content dataUsingEncoding: NSUTF8StringEncoding] attributes:nil];
NSFileManager-读取内容
NSData *fileData = [fileManager contentsAtPath:filePath];
NSString *content = [[NSString alloc] initWithData:fileData dataUsingEncoding: NSUTF8StringEncoding];
NSData-读取内容
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSData *data = [NSData dataWithContentOfFile:filePath];
NSString-读取内容
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSString *content = [[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
移动、拷贝文件
移动文件(重命名)
NSString *toPath =
[NSHomeDirectory( ) stringByAppendingPathComponent:@"hellogod/New Testament.txt"];
[fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
NSError *error;
BOOL isSuccess = [fm moveItemAtPath:filePath toPath:toPath error:&error];
拷贝文件(重命名)
NSString *copyPath =
[NSHomeDirectory( ) stringByAppendingPathComponent:@"备份/Old Testament.txt"];
[fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
BOOL success = [fm copyItemAtPath:toPath toPath:toPath error:nil];
删除文件、获取文件大小
推断文件是否存在和删除文件
if([fm fileExistsAtPath])
{
if ([fm removeItemAtPath:copyPath])
{
NSLog(@"remove success");
}
}
获取文件大小
NSFileManager *fileManager = [NSFileManager defaultManager];
获得文件的属性字典
NSDictionary *attrDic = [fileManager attributesOfItemAtpath:sourcePath error:nil];
NSNumber *fileSize = [attrDic objectForKey:NSFileSize];
获取文件夹文件信息
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *enuPath =
[NSHomeDirectoty( ) stringByAppendingPathComponent:@"Test"]; NSDictionaryEnumerator *dirEnum = [fileManager enumeratorAtPath:enuPath];
NSString *path = nil;
while ((path = [dirEnum nextObject]} != nil)
{ NSLog(@"%@",path);
}
郝萌主倾心贡献,尊重作者的劳动成果。请勿转载。
假设文章对您有所帮助。欢迎给作者捐赠。支持郝萌主,捐赠数额任意。重在心意^_^
我要捐赠: 点击捐赠
Cocos2d-X源代码下载:点我传送
游戏官方下载:
http://dwz.cn/RwTjl
游戏视频预览:
http://dwz.cn/RzHHd
游戏开发博客:
http://dwz.cn/RzJzI
游戏源代码传送:
http://dwz.cn/Nret1
iOS开发-文件管理之多的是你不知道的事(一)的更多相关文章
- IOS开发-文件管理(二)
IOS开发-文件管理(二) 五.Plist文件 String方式添加 NSString *path = [NSHomeDirectory( ) stringByAppen ...
- iOS开发-文件管理(一)
iOS开发-文件管理(一) 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.pli ...
- 【转】iOS开发-文件管理(一)
iOS开发-文件管理(一) 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.pli ...
- iOS开发-文件管理
iOS学习笔记(十七)--文件操作(NSFileManager) 浅析 RunLoop 解决EXC_BAD_ACCESS错误的一种方法--NSZombieEnabled iOS开发--Swift篇&a ...
- iOS开发笔记15:地图坐标转换那些事、block引用循环/weak–strong dance、UICollectionviewLayout及瀑布流、图层混合
1.地图坐标转换那些事 (1)投影坐标系与地理坐标系 地理坐标系使用三维球面来定义地球上的位置,单位即经纬度.但经纬度无法精确测量距离戒面积,也难以在平面地图戒计算机屏幕上显示数据.通过投影的方式可以 ...
- iOS开发系列—Objective-C之Foundation框架
概述 我们前面的章节中就一直新建Cocoa Class,那么Cocoa到底是什么,它和我们前面以及后面要讲的内容到底有什么关系呢?Objective-C开发中经常用到NSObject,那么这个对象到底 ...
- iOS开发----优秀文章推荐
UI界面 iOS和Android 界面设计尺寸规范 http://www.alibuybuy.com/posts/85486.html iPhone app界面设计尺寸规范 http://www. ...
- iOS开发中的4种数据持久化方式【一、属性列表与归档解档】
iOS中的永久存储,也就是在关机重新启动设备,或者关闭应用时,不会丢失数据.在实际开发应用时,往往需要持久存储数据的,这样用户才能在对应用进行操作后,再次启动能看到自己更改的结果与痕迹.ios开发中, ...
- 【转】iOS 开发怎么入门?
原文网址:http://www.zhihu.com/question/20264108 iOS 开发怎么入门? 请问有设计模式.内存管理方面的资料吗?最好有除了官方文档之外的其它内容,10 条评论 分 ...
随机推荐
- Task.Factory.StartNew多线程中将数值实时传递到UI显示
private void button1_Click(object sender, EventArgs e) { Task t1 = Task.Factory.StartNew(() => k1 ...
- python爬虫:读取PDF
下面的代码可以实现用python读取PDF,包括读取本地和网络上的PDF. pdfminer下载地址:https://pypi.python.org/packages/source/p/pdfmine ...
- ACM_____不再爱你……
不再爱你…… 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 现在有一个圆柱形水杯,里面装满了水,在它的底部有一个小洞,通过一些简单的物理知识我们可以知道: 1.由于重力 ...
- java keytool证书工具使用小结(转载)
原文地址:http://www.micmiu.com/lang/java/keytool-start-guide/ Keytool 是一个Java数据证书的管理工具 ,Keytool将密钥(key)和 ...
- window环境下,提升工作效率
效率工具 windows桌面程序 Listary 本地文件搜索器 有道词典 划词翻译, 并且能够存在生词本里面 印象笔记 浏览资料整理 chrom插件 crxMouse 鼠标手势 chrome vim ...
- .apply .call方法的区别及使用 .apply第二个参数为数组,.call第二个参数为参数列表, 相同点:第一个参数都为Function函数内部的this对象.
Function.apply(obj,args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 args:这个是数组,它将作为参数传给Function(args--> ...
- 何使用ultraiso软碟通制作u盘启动盘(转载)
现在很多网友都不知道如何用UltraISO软件来制作制作u盘启动盘,那么今天U大师小编就来给大家简单的介绍两种方法,首先第一种方法就是网友要到网上下载一个UltraISO软件,这个网上有很多的 ...
- Linux常用命令速查
索引表格 命令 功能简述 目录与文件基本操作 pwd 显示当前目录 ls 列出目录和文件名称 cp 复制文件或目录 mv 移动或更名现有的文件或目录 rm 删除文件或目录 mkdir 新建目录 rmd ...
- BZOJ 1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏 幼儿园测试题
本来以为是一道数学题,一顿XJBT导式子,结果就是个幼儿园都会的模拟. Code: #include<bits/stdc++.h> #define ll long long using n ...
- IOS - Display a base64 image within a UIImageView: 显示一个base64的图片
base64字符串(base64String)-存的是image数据NSData* data = [[NSData alloc] initWithBase64EncodedString:base64S ...