ios 怎样取ios删除沙盒文件夹下的所有文件的名字

&&&&&&&&&&&&&&&&&&
posts - 42,comments - 6,trackbacks - 0
沙盒下主要有四个文件夹:document,caches,tmp,library
document 的路径 程序运行时生成的文件,这个文件不要存比较放大的文件,比如音频,视频类,因为这里的东西会被上传
caches 的路径 一般用于文件的下载,存储(不会被上传)
tmp 临时文件.程序结束后应该清空
沙盒文件夹路径获取:
获取沙盒路径:
1 NSString *sandBoxPath = NSHomeDirectory();
获取 document :
1 NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
获取 caches 路径:
1 NSString *cacherPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
获取 tmp 路径:
1 NSString *tmpPath = NSTemporaryDirectory();
在沙盒下创建文件:
1.获取路径
2.拼接文件名(包括后缀)
3.将内容写到文件
例:写一个 txt 文件:
1 //NSString 写入
NSString *str = @"二傻子";
//获取 document 路径
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//拼接上一个 txt 文件
NSString *filePath = [docPath stringByAppendingPathComponent:@"coco.txt"];
//吧字符串写到 txt 文件
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
例:写一个 array 文件:
1 //NSArray
NSArray *array = @[@"二傻子",@"三傻子",@"翠花",@"叶良岑",@"赵日天",@"王尼玛"];
NSString *tmpPath1 = NSTemporaryDirectory();
NSString *tmpFilePath = [tmpPath1 stringByAppendingPathComponent:@"tmp.plist"];
[array writeToFile:tmpFilePath atomically:YES];
例:写一个 dictionary 文件:
1 NSDictionary *dic = @{@"1号":@"XXXXX",@"2号":@"XXXXX",@"3号":@"XXXXX",@"4号":@"XXXXX",@"5号":@"XXXXX"};
3 NSString *dicPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
4 NSString *dicFilePath = [dicPath stringByAppendingPathComponent:@"dic.plist"];
6 [dic writeToFile:dicFilePath atomically:YES];
例:写一个 image 文件:
1 NSString *cacherPath2 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
3 NSString *imgFilePath = [cacherPath2 stringByAppendingPathComponent:@"123.png"];
4 5 NSData *data = [[NSData alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"]];
7 [data writeToFile:imgFilePath atomically:YES];
阅读(...) 评论()IOS获取各种文件目录路径的方法
字体:[ ] 类型:转载 时间:
ios获取文件路径的方法,iphone沙箱模型的四个文件夹,通过documents,tmp,app,Library得到模拟器路径的简单方式,下面小编整理相关资料,把IOS获取各种文件目录路径的方式总结如下,需要的朋友可以参考下
iphone沙箱模型有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么.
documents,tmp,app,Library。
(NSHomeDirectory()),
手动保存的文件在documents文件里
Nsuserdefaults保存的文件在tmp文件夹里
1、Documents 目录:您应该将所有de应用程序数据文件写入到这个目录下。这个目录用于存储用户数据或其它应该定期备份的信息。
2、AppName.app 目录:这是应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动。
3、Library 目录:这个目录下有两个子目录:Caches 和 Preferences
Preferences 目录:包含应用程序的偏好设置文件。您不应该直接创建偏好设置文件,而是应该使用NSUserDefaults类来取得和设置应用程序的偏好.
Caches 目录:用于存放应用程序专用的支持文件,保存应用程序再次启动过程中需要的信息。
4、tmp 目录:这个目录用于存放临时文件,保存应用程序再次启动过程中不需要的信息。
获取这些目录路径的方法:
第一种方式:获取家目录路径的函数:
NSString *homeDir = NSHomeDirectory();
第二种方式:获取Documents目录路径的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
第三种方式:获取Caches目录路径的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
第四种方式:获取tmp目录路径的方法:
NSString *tmpDir = NSTemporaryDirectory();
第五种方式:获取应用程序程序包中资源文件路径的方法:
例如获取程序包中一个图片资源(apple.png)路径的方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
代码中的mainBundle类方法用于返回一个代表应用程序包的对象。
iphone沙盒(sandbox)中的几个目录获取方式:
// 获取沙盒主目录路径&
NSString *homeDir = NSHomeDirectory();
// 获取Documents目录路径&
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
// 获取Caches目录路径&
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
// 获取tmp目录路径&
NSString *tmpDir = NSTemporaryDirectory();
// 获取当前程序包中一个图片资源(apple.png)路径&
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
NSFileManager* fm=[NSFileManager defaultManager];
if(![fm fileExistsAtPath:[self dataFilePath]]){
//下面是对该文件进行制定路径的保存
[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];
//取得一个目录下得所有文件名
NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];
//读取某个文件
NSData *data = [fm contentsAtPath:[self dataFilePath]];
NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];
ios获取文件路径的方法有多种,下面介绍一种IOS中获取文件路径比较简单方法。
网上的DOCUMNET和“教程”真让人越看越糊涂,还是自己记下吧。
首先把文件(比如本例中的testFile.txt文件)放置在resources分组下,然后代码这样写:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"testFile" ofType:@"txt"];
NSLog(@"data path: %@", filePath);
输出的日志中你可以看到testFile.txt的路径已经获得。
再举一个例子:连接SQLITE数据库
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"myData" ofType:@"sqlite"];
if (sqlite3_open([dataPath UTF8String], &db) != SQLITE_OK)
sqlite3_close(db);
NSLog(@"数据库打开失败");
NSLog(@"数据库成功打开");
以上内容是IOS获取各种文件目录路径的方法,希望对大家有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具iOS 沙盒(sandbox)机制和文件操作
iOS 沙盒(sandbox)机制和文件操作
本文参看了&http://www./mobiledev/.asp#1 这篇文章中的介绍,尊重原著。
1、IOS沙盒机制
IOS应用程序只能在本应用程序中创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等。
1.1、每个应用程序都有自己的存储空间
1.2、应用程序不能翻过自己的围墙去访问别的存储空间的内容
1.3、应用程序请求的数据都要通过权限检测,假如不符合条件的话,不会被放行。
通过这张图只能从表层上理解sandbox是一种安全体系,应用程序的所有操作都要通过这个体系来执行,其中核心内容是:sandbox对应用程序执行各种操作的权限限制。
2、打开模拟器沙盒目录
下面看看模拟器的沙盒文件夹在mac电脑上的什么位置。
文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,英文名是Library。
下面介绍一种简单方法前往该文件夹:在Finder上点-&前往-&前往文件夹
进入模拟器后,里面就包含了各个应用程序的沙盒。
进入一个应用程序,如下图,就是一个沙箱了。
下面介绍一下沙箱的目录结构:
默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp和一个应用程序文件(也是一个文件)。因为应用的沙盒机制,应用只能在几个目录下读写文件
Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
tmp:提供一个即时创建临时文件的地方。
iTunes在与iPhone同步时,备份所有的Documents和Library文件。
iPhone在重启时,会丢弃所有的tmp文件。
注意:这里很容易和bundle混淆在一起,下面根据自己的一点理解说明二者的区别:
bundle :生成 iOS 应用程序时,Xcode 将它捆绑成一个包。捆绑包 (bundle) 是文件系统中的一个目录,它将相关资源成组在一个地方。一个 iOS 应用程序捆绑包中,含有其可执行文件和支持资源文件(如应用程序图标、图像文件和已本地化的内容)。
A bundle(包裹、捆、束) is a directory with a standardizedhierarchical structure that holds executable code and the resources used by that code.
所以可以将整个应用程序其实就可以看做一个bundle。
沙箱的概念和bundle没直接关系,沙箱只是说明程序资源与外界隔离
下面通过一个简单的例子说明一下bundle和sandbox。
//新建的plist文件是在应用程序中的,可以通过bundle存取到该文件
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:plistPath];
//向数组中新添加一个项目
[array addObject:@"3"];
//重新写回plist文件中
BOOL value = [array writeToFile:plistPath atomically:YES];
if (value) {
NSMutableArray *newArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSLog(@"new array = %@",newArray);
new array = (
//获取沙箱中document的path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
//将数组写入到沙箱的document中的data.plist文件中
[array writeToFile:newPath atomically:YES];
NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:newPath];
NSLog(@"array in data.plist = %@",arr);
array in data.plist = (
说明:我们首先在项目中新建一个plist文件(root项的类型为数组),添加了3个元素。因为新建的plist文件是在应用程序中的,我们可以通过bundle获取到这个plist文件,读取出这个数组,添加一个数据元素后,重新写回plist文件中。接着我们获取沙箱document的path,然后将这个文件写入到沙箱中的data.plist文件中(如果不存在,会自动新建一个的),然后再从data.plist读取出这个数组。
关于新建的MyPlist.plist文件,我们写回文件的数组中添加了一项新的元素,但是我们在xcode中查看这个MyPlist.plist文件时,发现并没有显示出新增的数组元素,但是我们到沙箱中查看就可以看到了,这个估计是xoode本身的问题。
关于document中data.plist文件查看我们也可以到沙箱中进行查看。如下图:
3、获取沙盒目录:
//1、获取程序的Home目录
NSString *homeDirectory = NSHomeDirectory();
NSLog(@"path:%@", homeDirectory);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A--3E46E41CC671
//2、获取document目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A--3E46E41CC671/Documents
//3、获取Cache目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A--3E46E41CC671/Library/Caches
//4、获取Library目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A--3E46E41CC671/Library
//5、获取tmp目录
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"path:%@", tmpDir);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A--3E46E41CC671/tmp/
4、文件操作之NSFileManager
4.1 、在document中创建一个文件目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"documentsDirectory%@",documentsDirectory);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
// 创建目录
[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
4.2 、 在test目录下创建文件
创建文件怎么办呢?接着上面的代码 testPath 要用stringByAppendingPathComponent拼接上你要生成的文件名,比如test11.txt。这样才能在test目录下写入文件。
testDirectory是上面代码生成的路径哦,不要忘了。我往test文件夹里写入三个文件,test11.txt ,test22.txt,text.33.txt。内容都是写入内容,write String。
实现代码如下:
NSString *testPath1 = [testDirectory stringByAppendingPathComponent:@"test1.txt"];
NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test2.txt"];
NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test3.txt"];
NSString *string = @"写入内容,write String";
[fileManager createFileAtPath:testPath1 contents:[string
dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
[fileManager createFileAtPath:testPath2 contents:[string
dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
[fileManager createFileAtPath:testPath3 contents:[string
dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
4.3获取目录列里所有文件名
两种方法获取:subpathsOfDirectoryAtPath 和 subpathsAtPath
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"documentsDirectory%@",documentsDirectory);
NSFileManager *fileManage = [NSFileManager defaultManager];
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];
NSLog(@"%@",file);
NSArray *files = [fileManage subpathsAtPath: myDirectory ];
NSLog(@"%@",files);
获取刚才test目录下的所以文件名:
两种方法都是输出
"test1.txt",
"test2.txt",
"test3.txt"
、fileManager使用操作当前目录
//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//更改到待操作的目录下
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
NSString * fileName = @"testFileNSFileManager.txt";
NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil];
//下面是将数组类型转换为NSData类型
NSMutableData *data = [[NSMutableData alloc] init];
for (int i = 0; i & [array count]; ++i ){
NSString *str = [array objectAtIndex:i];
NSData *temp = [str dataUsingEncoding:NSUTF8StringEncoding];
[data appendData:temp];
//注意contents参数的类型是NSData类型
[fileManager createFileAtPath:fileName contents:data attributes:nil];
4.5 删除文件
接着上面的代码就可以将刚新建的&testFileNSFileManager.txt文件删除!
[fileManager removeItemAtPath:fileName error:nil];
4.6 混合数据的读写 &请参看原文最后面的内容。
大概就是这么多了吧!
发表评论:
TA的最新馆藏1971人阅读
iOS开发(107)
将所有文件存入数组
NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *tempFileList = [[NSArray alloc] initWithArray:[fileManager contentsOfDirectoryAtPath:string error:nil]];
移除所有文件
NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@&Documents&];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:DocumentsPath];
for (NSString *fileName in enumerator) {
[[NSFileManager defaultManager] removeItemAtPath:[DocumentsPath stringByAppendingPathComponent:fileName] error:nil];
移除所有png文件
NSString *extension = @&png&;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSEnumerator *enumerator = [contents objectEnumerator];
NSString *
while ((filename = [enumerator nextObject])) {
if ([[filename pathExtension] isEqualToString:extension]) {
[fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:nil];
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:79563次
积分:2132
积分:2132
排名:第13981名
原创:102篇
转载:11篇
评论:17条
(1)(11)(37)(56)(9)iOS怎么获取并移除沙盒中Documents文件夹下的所有文件
iOS怎么获取并移除沙盒中Documents文件夹下的所有文件
将所有文件存入数组
NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *tempFileList = [[NSArray alloc] initWithArray:[fileManager contentsOfDirectoryAtPath:string error:nil]];
移除所有文件
NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@&Documents&];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:DocumentsPath];
for (NSString *fileName in enumerator) {
[[NSFileManager defaultManager] removeItemAtPath:[DocumentsPath stringByAppendingPathComponent:fileName] error:nil];
移除所有png文件
NSString *extension = @&png&;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSEnumerator *enumerator = [contents objectEnumerator];
NSString *
while ((filename = [enumerator nextObject])) {
if ([[filename pathExtension] isEqualToString:extension]) {
[fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:nil];
我的热门文章
即使是一小步也想与你分享}

我要回帖

更多关于 ios 文件写入沙盒 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信