- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 沙盒(SandBox)
// Documents(文件文档, 用户主动数据存储)
// Libray(资源, 一般用来存放, 程序员要存储的一些数据)
// ⬇️
// Cache (缓存文件)
// Perferences (用户信息和一些用户设置, NSUserDefaults)
// tmp(临时目录, 下载的临时文件一般放这里) [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
[[NSUserDefaults standardUserDefaults] synchronize]; // 2. 获取沙盒路径
// 下面是两个快捷获取到目录的 C 语言的函数
// 根目录 家目录
NSHomeDirectory();
NSLog(@"Home------%@", NSHomeDirectory());
// 临时目录 tmp 目录
NSTemporaryDirectory();
NSLog(@"Temporary-----%@", NSTemporaryDirectory()); // C 函数
// 参数1: 搜索文件夹路径 NSSearchPathDirectory
// 常用: NSDocumentDirectory NSLibraryDirectory NSCachesDirectory
// 参数2: 在用户作用域下搜索
// 参数3: YES or NO YES代表绝对路径(基本上用绝对路径), NO代表相对路径(~)
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"%@", pathArray);
[pathArray firstObject]; // NSBundle .app文件包
NSLog(@"%@", [NSBundle mainBundle]); // 1> 简单的文件读写 Input Output
NSString *hello = @"Hello, I/O";
// 一般拼接路径时, 使用 stringByAppendingPathComponent 会自动加斜杠
NSString *writePath = [[pathArray firstObject] stringByAppendingPathComponent:@"hello.txt"];
NSError *error = nil;
[hello writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"存储失败");
} else {
NSLog(@"存储成功");
} // 2> 读取路径对应的文字
NSError *readError = nil;
NSString *readString = [NSString stringWithContentsOfFile:writePath encoding:NSUTF8StringEncoding error:&readError];
NSLog(@"%@", readString); // 3> 将 数组 写入本地文件
NSArray *array = @[@"黄航", @"韩旭", @"爆花", @"宝宝"];
NSString *arrayPath = [[pathArray firstObject] stringByAppendingPathComponent:@"name.plist"];
BOOL isArrayWriteSuccess = [array writeToFile:arrayPath atomically:YES];
if (isArrayWriteSuccess) {
NSLog(@"写入成功");
} else {
NSLog(@"写入失败");
} // 4> 将 数组 读取
NSArray *nameArray = [NSArray arrayWithContentsOfFile:arrayPath];
NSLog(@"%@", nameArray); // 5> 将 字典 写入本地
NSDictionary *dict = @{@"name":@"mafeng",
@"age":@"",
@"sex":@"man"};
NSString *dictPath = [[pathArray firstObject] stringByAppendingPathComponent:@"mafeng.plist"];
BOOL isDictWriteSuccess = [dict writeToFile:dictPath atomically:YES];
if (isDictWriteSuccess) {
NSLog(@"写入成功");
} else {
NSLog(@"写入失败");
} // 6> 将字典读取出来
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dictPath];
NSLog(@"%@", dic); // 7> 将Data类型写入本地
UIImage *image = [UIImage imageNamed:@"user"]; NSString *dataPath = [[pathArray firstObject] stringByAppendingPathComponent:@"imageData"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.1); BOOL isDataWriteSuccess = [imageData writeToFile:dataPath atomically:YES];
NSLog(@"%@", imageData);
if (isDataWriteSuccess) {
NSLog(@"写入成功");
} else {
NSLog(@"写入失败");
} NSData *imageNewData = [NSData dataWithContentsOfFile:dataPath];
UIImage *fileImage = [UIImage imageWithData:imageNewData]; // 2. 复杂对象文件读写, 自定义类型
// 归档/反归档, 序列化/反序列化 // 1> 归档, 将 对象 存储到本地
Book *book = [Book new];
book.bookName = @"放弃iOS从我做起";
book.bookType = @"教育";
book.bookPrice = @"988.5";
book.bookAuthor = @"晃晃";
book.bookAddress = @"演变大学"; NSString *bookPath = [[pathArray firstObject] stringByAppendingPathComponent:@"book.plist"];
BOOL isSuccess = [NSKeyedArchiver archiveRootObject:book toFile:bookPath];
if (isSuccess) {
NSLog(@"写入成功");
} // 2> 反归档
Book *huangBook = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
NSLog(@"%@", huangBook.bookName); // 如果对象想要实现归档和反归档
// 1. 对象对应的类需要签订 Coding
// 2. 实现写一方法
// 1> initWithCoder 反归档用
// 2> encodeWithCoder 归档用
// 3. 归档时使用 KeyedArchiver
// 4. 反归档时, 使用 KeyedUnarchiver // 创建一个文件管理器
NSFileManager *manager = [NSFileManager defaultManager];
NSString *filePath = [[pathArray firstObject] stringByAppendingPathComponent:@""];
// 创建文件夹
[manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
// 文件是否存在
BOOL isExists = [manager fileExistsAtPath:filePath];
// 删除文件
BOOL isDele = [manager removeItemAtPath:bookPath error:nil];
if (isDele) {
NSLog(@"删除成功");
} else {
NSLog(@"删除失败");
} if (isExists) {
NSLog(@"文件夹存在");
// 拷贝文件
NSString *copyPath = [filePath stringByAppendingPathComponent:@"dict.plist"];;
BOOL isCopy = [manager copyItemAtPath:dictPath toPath:copyPath error:nil];
if (isCopy) {
NSLog(@"拷贝成功");
} else {
NSLog(@"拷贝失败");
}
// 移动文件
NSString *movePath = [filePath stringByAppendingPathComponent:@"mov.plist"];;
BOOL isMove = [manager moveItemAtPath:dictPath toPath:movePath error:nil];
if (isMove) {
NSLog(@"移动成功");
} else {
NSLog(@"移动失败");
} } else {
NSLog(@"文件夹不存在");
} return YES;
}

iOS文件和文件夹的创建,删除,移动, 拷贝,是否存在及简单数据类型的读写的更多相关文章

  1. net8:简易的文件磁盘管理操作一(包括文件以及文件夹的编辑创建删除移动拷贝重命名等)

    原文发布时间为:2008-08-07 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  2. net8:简易的文件磁盘管理操作二(包括文件以及文件夹的编辑创建删除移动拷贝重命名等)

    原文发布时间为:2008-08-07 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  3. Linux_文件及文件夹[创建][复制][移动][删除][重命名]

    一.文件/文件夹创建 1.文件的创建 touch , vi/vim/nano , ... 语   法: touch [-acfm][-d <日期时间>][-r <参考文件或目 录&g ...

  4. ubuntu创建、删除文件及文件夹方法

    mkdir 目录名         => 创建一个目录 rmdir 空目录名      => 删除一个空目录 rm 文件名 文件名   => 删除一个文件或多个文件 rm –rf 非 ...

  5. ubuntu创建、删除文件及文件夹,强制清空回收站方法

    mkdir 目录名         => 创建一个目录 rmdir 空目录名      => 删除一个空目录 rm 文件名 文件名   => 删除一个文件或多个文件 rm –rf 非 ...

  6. linux下文件夹的创建、复制、剪切、重命名、清空和删除命令

    在home目录下有wwwroot目录,wwwroot下有sinozzz目录,即/home/wwwroot/sinozzz 一.目录创建 在/home/wwwroot目录下新建一个sinozzz123的 ...

  7. Linux 删除文件夹和创建文件的命令

    删除文件夹实例:rm -rf /var/log/httpd/access将会删除/var/log/httpd/access目录以及其下所有文件.文件夹 删除文件使用实例: rm -f /var/log ...

  8. (转载)ubuntu创建、删除文件及文件夹,强制清空回收站方法

    mkdir 目录名         => 创建一个目录 rmdir 空目录名      => 删除一个空目录 rm 文件名 文件名   => 删除一个文件或多个文件 rm –rf 非 ...

  9. Java创建、重命名、删除文件和文件夹(转)

    Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了.如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归. 下面是的一个解决方 ...

随机推荐

  1. linux磁盘占用跟每个文件夹大小总和不符

    1.一种情况是删除了大文件但是没有释放出来,因为有进程还在调用使用 最简单的方法是reboot下服务器再对比下: 2.查看服务器空间使用情况 df -h cd / du -sh *

  2. vue 开始开发

    1,引入vue.js文件 2,在body里用标签 编辑一个入口 <div id="app">{{msg}}</div> <-- 用双大括号 取数据显示 ...

  3. VMware Workstation虚拟机Ubuntu中实现与主机共享(复制和粘贴)

    VMware Workstation中安装虚拟机Ubuntu后,开始都不能与主机实现共享,即相互之间能实现复制粘贴的功能.要解决问题,只需要安装VMvare tools后然后重启虚拟机Ubuntu即可 ...

  4. Dubbo之旅--注册中心

    在介绍Dubbo的内部逻辑的时候提到很多次注册中心的概念.实现注册中心的有很多,主要是以下四个注册中心分别是: Multicast注册中心 Zookeeper注册中心 Redis注册中心 Simple ...

  5. java中的Map集合

    Map接口 Map为一个接口.实现Map接口的类都有一个特点:有键值对,将键映射到值的对象. Map不能包含重复的键,每个键可以映射到最多一个值. Map常见的接口方法有: V  put(K key, ...

  6. 简写代码:当变量为false时['',false,null,undefined,0,NaN]时,返回默认值

    当变量为'',false,null,undefined,0,NaN时,返回默认值 var a='' a || 'hello world'   "hello world" var a ...

  7. graylog安装

    官网:http://docs.graylog.org/en/2.4/pages/installation/os/centos.html Prerequisites Taking a minimal s ...

  8. sql server 2008 R2无法连接127.0.0.1报错 Server error:40(错误:53)

    在公司用sql server 2008 R2很好的,回家连接127.0.0.1就报错.sql server2008R2主机名和.都可以登录,连接127.0.0.1出错,在与 SQL Server 建立 ...

  9. OpenCL学习笔记(三):OpenCL安装,编程简介与helloworld

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. OpenCL安装 安装我不打算 ...

  10. poj 2452(RMQ+二分查找)

    题目链接: http://poj.org/problem?id=2452 题意:在区间[1,n]上找到满足 a[i]<a[k]<a[j] (i<=k<=j) 的最大子区间 (j ...