【代码笔记】iOS-NSFileManager
一,代码。

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//获取应用沙盒根路径
[self dirHome];
//获取Documents目录
[self dirDoc];
//获取Library目录
[self dirLib];
//获取Cache目录
[self dirCache];
//创建文件夹
[self createDir];
//创建文件
[self createFile];
//写数据到文件
[self writeFile];
//读文件
[self readFile];
//文件属性
[self fileAttriutes];
//删除文件
[self deleteFile];
}
#pragma -mark -funcitons
//获取应用沙盒根路径
-(void)dirHome{
NSString *dirHome=NSHomeDirectory();
NSLog(@"应用沙盒根路径: %@",dirHome);
}
//获取Documents目录
-(NSString *)dirDoc{
//[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Documents目录: %@",documentsDirectory);
return documentsDirectory;
}
//获取Library目录
-(void)dirLib{
//[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSLog(@"Library目录: %@",libraryDirectory);
}
//获取Cache目录
-(void)dirCache{
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
NSLog(@"Cache目录: %@",cachePath);
}
//获取Tmp目录
-(void)dirTmp{
//[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
NSString *tmpDirectory = NSTemporaryDirectory();
NSLog(@"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");
}
@end

二,输出。

2015-10-23 11:17:54.335 NSFileManager[5578:133206] 应用沙盒根路径: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980
2015-10-23 11:17:54.336 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.336 NSFileManager[5578:133206] Library目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Library
2015-10-23 11:17:54.337 NSFileManager[5578:133206] Cache目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Library/Caches
2015-10-23 11:17:54.337 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.337 NSFileManager[5578:133206] 文件夹创建成功
2015-10-23 11:17:54.337 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.349 NSFileManager[5578:133206] 文件创建成功: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents/test/test.txt
2015-10-23 11:17:54.349 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.350 NSFileManager[5578:133206] 文件写入成功
2015-10-23 11:17:54.350 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.351 NSFileManager[5578:133206] 文件读取成功: 测试写入内容!
2015-10-23 11:17:54.351 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.352 NSFileManager[5578:133206] Key: NSFileOwnerAccountID for value: 501
2015-10-23 11:17:54.352 NSFileManager[5578:133206] Key: NSFileSystemFileNumber for value: 13276863
2015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileExtensionHidden for value: 0
2015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileSystemNumber for value: 16777220
2015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileSize for value: 21
2015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileGroupOwnerAccountID for value: 80
2015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFilePosixPermissions for value: 420
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileCreationDate for value: 2015-10-23 03:17:54 +0000
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileExtendedAttributes for value: {
"com.apple.TextEncoding" = <7574662d 383b3133 34323137 393834>;
}
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileType for value: NSFileTypeRegular
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileGroupOwnerAccountName for value: admin
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileReferenceCount for value: 1
2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileModificationDate for value: 2015-10-23 03:17:54 +0000
2015-10-23 11:17:54.356 NSFileManager[5578:133206] Documents目录: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents
2015-10-23 11:17:54.356 NSFileManager[5578:133206] 文件删除成功
2015-10-23 11:17:54.356 NSFileManager[5578:133206] 文件是否存在: NO

【代码笔记】iOS-NSFileManager的更多相关文章
- IOS开发笔记 IOS如何访问通讯录
IOS开发笔记 IOS如何访问通讯录 其实我是反对这类的需求,你说你读我的隐私,我肯定不愿意的. 幸好ios6.0 以后给了个权限控制.当打开app的时候你可以选择拒绝. 实现方法: [plain] ...
- 【hadoop代码笔记】Mapreduce shuffle过程之Map输出过程
一.概要描述 shuffle是MapReduce的一个核心过程,因此没有在前面的MapReduce作业提交的过程中描述,而是单独拿出来比较详细的描述. 根据官方的流程图示如下: 本篇文章中只是想尝试从 ...
- 【hadoop代码笔记】hadoop作业提交之汇总
一.概述 在本篇博文中,试图通过代码了解hadoop job执行的整个流程.即用户提交的mapreduce的jar文件.输入提交到hadoop的集群,并在集群中运行.重点在代码的角度描述整个流程,有些 ...
- 【Hadoop代码笔记】目录
整理09年时候做的Hadoop的代码笔记. 开始. [Hadoop代码笔记]Hadoop作业提交之客户端作业提交 [Hadoop代码笔记]通过JobClient对Jobtracker的调用看详细了解H ...
- 笔记-iOS 视图控制器转场详解(上)
这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...
- <Python Text Processing with NLTK 2.0 Cookbook>代码笔记
如下是<Python Text Processing with NLTK 2.0 Cookbook>一书部分章节的代码笔记. Tokenizing text into sentences ...
- [学习笔记] SSD代码笔记 + EifficientNet backbone 练习
SSD代码笔记 + EifficientNet backbone 练习 ssd代码完全ok了,然后用最近性能和速度都非常牛的Eifficient Net做backbone设计了自己的TinySSD网络 ...
- DW网页代码笔记
DW网页代码笔记 1.样式. class 插入类样式 标签技术(html)解决页面的内容样式技术(css)解决页面的外观脚本技术 解决页面动态交互问题<form> ...
- 前端学习:JS(面向对象)代码笔记
前端学习:JS(面向对象)代码笔记 前端学习:JS面向对象知识学习(图解) 创建类和对象 创建对象方式1调用Object函数 <body> </body> <script ...
- 离屏渲染学习笔记 /iOS圆角性能问题
离屏渲染学习笔记 一.概念理解 OpenGL中,GPU屏幕渲染有以下两种方式: On-Screen Rendering 意为当前屏幕渲染,指的是GPU的渲染操作是在当前用于显示的屏幕缓冲区中进行. O ...
随机推荐
- css经典布局—Sticky footers布局
参考:http://www.w3cplus.com/CSS3/css-secrets/sticky-footers.html 效果:将footer固定到底部.文章内容不足满屏时 footer在底部,超 ...
- flask——包含,继承,宏
包含,继承,宏 都是为了提高代码的效率,都是为了防止代码的沉余,浪费资源 宏(macro) 可以把它看做Jinja2中的一个函数,他会返回一个模板或者HTML字符串,为了避免反复的编写同样的模板代 ...
- 找到IIS 站点对应的站点日志
IIS6 下 IIS7 下 1 找到日志文件的路径 2 找到站点ID 3 打开日志文件路径,找到站点ID 对应的日志文件夹.文件夹的最后一位数字,就对应着站点ID.
- Centos6.7配置Nginx+Tomcat简单整合
系统环境:Centos 6.7 软件环境:JDK-1.8.0_65.Nginx-1.10.3.Tomcat-8.5.8 文档环境:/opt/app/ 存放软件目录,至于mkdir创建文件就不用再说了 ...
- 多线程之CountDownLatch和CyclicBarriar使用
CountDownLatch和CyclicBarriar是java.util.concurrent包下面提供的多线程同步工具,两者有点相似,相当于计数器,但是用处还是有区别的. CountDownLa ...
- 深入理解SpringCloud之Eureka注册过程分析
eureka是一种去中心化的服务治理应用,其显著特点是既可以作为服务端又可以作为服务向自己配置的地址进行注册.那么这篇文章就来探讨一下eureka的注册流程. 一.Eureka的服务端 eureka的 ...
- TensorFlow object detection API应用--配置
目标检测在图形识别的基础上有了更进一步的应用,但是代码也更加繁琐,TensorFlow专门为此开设了一个object detection API,接下来看看怎么使用它. object detectio ...
- Solidity的自定义结构体深入详解
一.结构体定义 结构体,Solidity中的自定义类型.我们可以使用Solidity的关键字struct来进行自定义.结构体内可以包含字符串,整型等基本数据类型,以及数组,映射,结构体等复杂类型.数组 ...
- android app启动过程
Native进程的运行过程 一般程序的启动步骤,可以用下图描述.程序由内核加载分析,使用linker链接需要的共享库,然后从c运行库的入口开始执行. 通常,native进程是由shell或者init启 ...
- 复刻smartbits的国产网络测试工具minismb-如何测试DPI引擎
复刻smartbits的网络性能测试工具MiniSMB,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此以太网测试工具测试任何ip网络设备的端口吞吐率,带宽,并发 ...