【代码笔记】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 ...
随机推荐
- 10-02 Java 形式参数和返回值的问题深入研究,链式编程
形式参数和返回值的问题: 1:形式参数和返回值的问题(理解) (1)形式参数: 类名:需要该类的对象 抽象类名:需要该类的子类对象 接口名:需要该接口的实现类对象 (2)返回值类型: 类名:返回的是该 ...
- DockPanel与GeckoFX、ChrominumFX、CefSharp结合使用问题
在使用DockPanel与ChrominumFx时,当在以下条件下拖动窗体时,会发生ChromiumWebBrowser崩溃的情况,此种情况也会在DockPanel与GeckoFX或CefSharp结 ...
- PHP:判断客户端是否使用代理服务器及其匿名级别
要判断客户端是否使用代理服务器,可以从客户端所发送的环境变量信息来判断. 具体来说,就是看HTTP_VIA字段,如果这个字段设置了,说明客户端使用了代理服务器. 匿名级别可以参考下表来判断. 给出一个 ...
- Shell的并发
#!/bin/bash ./step1.sh & >中文 i=$! ./step2.sh & >西王 j=$! wait #echo ${i} #echo ${j} ech ...
- 解决docker镜像无法下载的问题
从daocloud.io中找到了获取镜像的方式,在镜像仓库中可以找到镜像的地址,其他镜像地址可以以此类推: # docker pull daocloud.io/library/centos:lates ...
- postgresql 清空数据表数据
在 mysql中,只需要执行: TRUNCATE table_name; 即可,数据会情况,而且自增id也会变回0: 但在 postgresql 则稍有不同,因为 postgresql 的自增id是通 ...
- “网红架构师”解决你的Ceph 运维难题
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由Tstack发表于云+社区专栏 本文为长篇连续剧,将分多个篇幅发表,主要介绍了从动手部署环境到后期运营故障处理过程中常见的问题,内容由 ...
- 查看Linux 版本
如何得知自己正在使用的linux是什么版本呢,下面的几种方法将给你带来答案! 1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux v ...
- Storm1.0.6环境搭建
1.配置 三台服务器搭建Storm集群:CentOS7One,CentOS7Two,CentOS7Three 在CentOS7One机器上配置 1.1 zookeeper配置 目录:/opt/zook ...
- smarty 模板标签
smarty 模板标签 变量标签 数组变量标签 变量调节器 条件标签 循环标签 数组变量标签 模板加载标签 预定义变量标签 常量标签