iOS开发--沙盒路径与操作文件
获取应用沙盒根路径:
- -(void)dirHome{
- NSString *dirHome=NSHomeDirectory();
- NSLog(@"app_home: %@",dirHome);
- }
获取Documents目录路径:
- //获取Documents目录
- -(NSString *)dirDoc{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSLog(@"app_home_doc: %@",documentsDirectory);
- return documentsDirectory;
- }
获取Library目录路径:
- //获取Library目录
- -(void)dirLib{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
- NSString *libraryDirectory = [paths objectAtIndex:0];
- NSLog(@"app_home_lib: %@",libraryDirectory);
- }
获取Cache目录路径:
- //获取Cache目录
- -(void)dirCache{
- NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *cachePath = [cacPath objectAtIndex:0];
- NSLog(@"app_home_lib_cache: %@",cachePath);
- }
获取Tmp目录路径:
- //获取Tmp目录
- -(void)dirTmp{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
- NSString *tmpDirectory = NSTemporaryDirectory();
- NSLog(@"app_home_tmp: %@",tmpDirectory);
- }
创建文件夹:
- //创建文件夹
- -(void *)createDir{
- NSString *documentsPath =[self dirDoc];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- // 创建目录
- BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
- if (res) {
- NSLog(@"文件夹创建成功");
- }else
- NSLog(@"文件夹创建失败");
- }
创建文件
- //创建文件
- -(void *)createFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
- if (res) {
- NSLog(@"文件创建成功: %@" ,testPath);
- }else
- NSLog(@"文件创建失败");
- }
写数据到文件:
- //写文件
- -(void)writeFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- NSString *content=@"测试写入内容!";
- BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- if (res) {
- NSLog(@"文件写入成功");
- }else
- NSLog(@"文件写入失败");
- }
读文件数据:
- //读文件
- -(void)readFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- // NSData *data = [NSData dataWithContentsOfFile:testPath];
- // NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
- NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
- NSLog(@"文件读取成功: %@",content);
- }
文件属性:
- //文件属性
- -(void)fileAttriutes{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
- NSArray *keys;
- id key, value;
- keys = [fileAttributes allKeys];
- int count = [keys count];
- for (int i = 0; i < count; i++)
- {
- key = [keys objectAtIndex: i];
- value = [fileAttributes objectForKey: key];
- NSLog (@"Key: %@ for value: %@", key, value);
- }
- }
删除文件:
- //删除文件
- -(void)deleteFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- BOOL res=[fileManager removeItemAtPath:testPath error:nil];
- if (res) {
- NSLog(@"文件删除成功");
- }else
- NSLog(@"文件删除失败");
- NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
- }
iOS开发--沙盒路径与操作文件的更多相关文章
- iOS开发 沙盒路径和使用
1.模拟器沙盒目录文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library.因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件: ...
- iOS 沙盒路径获取,创建文件
沙盒下主要有四个文件夹:document,caches,tmp,library document 的路径 程序运行时生成的文件,这个文件不要存比较放大的文件,比如音频,视频类,因为这里的东西会被上传 ...
- iOS开发--沙盒
IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件 ...
- iOS开发-沙盒(sandbox)机制
苹果前天发的财报,貌似现在用ios系统的比以前又多了一些,但是大家的iPhone购买的渠道也是五花八门,有的从非正规渠道购买的iPhone里的操作系统已经被越狱过,越狱这个事情和Android的roo ...
- iOS 获取沙盒路径方法
//获取家目录路径的函数: NSString *homeDir = NSHomeDirectory(); //获取Documents目录路径的方法: NSArray *paths = NSSearch ...
- 关于ios项目沙盒中的文件和Xcode项目创建的文件
//1.1获取在Xcode项目打开的情况下创建的Plist文件 NSString *path = [[NSBundle mainBundle]pathForResource:@"Profes ...
- iOS开发之获取沙盒路径
iOS开发之沙盒机制(SandBox)具体解说了沙盒的一些机制.在开发中,我们须要对沙盒进行操作.所以我们须要获取到沙盒路径. 沙盒里的目录包含Documents.Library.tmp.这三个目录的 ...
- iOS 沙盒(sandbox)机制和文件操作
本文参看了 http://www.uml.org.cn/mobiledev/201209211.asp#1 这篇文章中的介绍,尊重原著. 1.IOS沙盒机制 IOS应用程序只能在本应用程序中创建的文件 ...
- IOS学习之IOS沙盒(sandbox)机制和文件操作
IOS学习之IOS沙盒(sandbox)机制和文件操作(一) 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都 ...
随机推荐
- [读行者][学习LinqExpression和Reflection(Emit)]阅读TypeBuilderSample之ExampleFromTheArticle
前言 关于”读行者“ 俗语有云:"读万卷书,行万里路“.多读一些优秀代码,不仅可以锻炼我们读代码的能力(便于维护或相互交流),还可以吸取很多我们成长所需的知识点.多读,才能开阔我们的眼界,才 ...
- jQuery4ajax
1.jQuery对ajax编程的支持 (1)load方法: 作用:将服务器返回的数据直接添加到符合要求的dom节点之上. 用法:load(url,[data]); url:请求地址,比如"p ...
- webpack对样式的处理
原文地址:https://github.com/zhengweikeng/blog/issues/9 我们可以在js中引入样式文件 require('myStyle.css') 这时我们便需要引入相应 ...
- 1.1 MySQL 逻辑架构
- 【ConnerStone】SVN代码管理 - 基本使用
第一步,链接服务器,创建代码管理仓库 第二步,输入服务器的配置,链接服务器(例子是以svn:// 为例子) 第三部 ,链接成功后,SVN的基本界面组成 第四步 从仓库中check out你需要的项目 ...
- 微软职位内部推荐-Senior Development Lead
微软近期Open的职位: MSIT Dynamics CRM Sr. Dev Lead (Sr. Dev Lead, Microsoft China, Beijing) Are you interes ...
- Log4Net学习【三】
Log4Net配置详解 配置方式一 在相应的应用程序的配置文件中配置,(WinForm对应的是*.exe.config,WebForm对应的是*.config),本实例是Web应用程序,以Web.co ...
- 使用个推的时候出现Installation error: INSTALL_FAILED_DUPLICATE_PERMISSION
使用个推的时候出现 Installation error: INSTALL_FAILED_DUPLICATE_PERMISSION perm=getui.permissio... 解决办法: 先将手机 ...
- BZOJ3503: [Cqoi2014]和谐矩阵
题解: 如果第一行的数知道了,我们就可以推出其他行的数. 那么如何判断第一行的数的一种填法是否合法呢?很简单,我们递推出m+1行的数,当且仅当这一行都是0时满足题意. 那么,我们就有了一种想法. 直接 ...
- DepthClipEnable error
刚刚呢又遇到这种鬼扯的问题,ps就return个(1,1,0,1) nisight的汇编都写对了,但结果就是画不出任何东西,按照经验,必然是某个state.... 我就找啊找啊,被我找到一个 Rast ...