iOS文件和文件夹的创建,删除,移动, 拷贝,是否存在及简单数据类型的读写
- (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文件和文件夹的创建,删除,移动, 拷贝,是否存在及简单数据类型的读写的更多相关文章
- net8:简易的文件磁盘管理操作一(包括文件以及文件夹的编辑创建删除移动拷贝重命名等)
原文发布时间为:2008-08-07 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- net8:简易的文件磁盘管理操作二(包括文件以及文件夹的编辑创建删除移动拷贝重命名等)
原文发布时间为:2008-08-07 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- Linux_文件及文件夹[创建][复制][移动][删除][重命名]
一.文件/文件夹创建 1.文件的创建 touch , vi/vim/nano , ... 语 法: touch [-acfm][-d <日期时间>][-r <参考文件或目 录&g ...
- ubuntu创建、删除文件及文件夹方法
mkdir 目录名 => 创建一个目录 rmdir 空目录名 => 删除一个空目录 rm 文件名 文件名 => 删除一个文件或多个文件 rm –rf 非 ...
- ubuntu创建、删除文件及文件夹,强制清空回收站方法
mkdir 目录名 => 创建一个目录 rmdir 空目录名 => 删除一个空目录 rm 文件名 文件名 => 删除一个文件或多个文件 rm –rf 非 ...
- linux下文件夹的创建、复制、剪切、重命名、清空和删除命令
在home目录下有wwwroot目录,wwwroot下有sinozzz目录,即/home/wwwroot/sinozzz 一.目录创建 在/home/wwwroot目录下新建一个sinozzz123的 ...
- Linux 删除文件夹和创建文件的命令
删除文件夹实例:rm -rf /var/log/httpd/access将会删除/var/log/httpd/access目录以及其下所有文件.文件夹 删除文件使用实例: rm -f /var/log ...
- (转载)ubuntu创建、删除文件及文件夹,强制清空回收站方法
mkdir 目录名 => 创建一个目录 rmdir 空目录名 => 删除一个空目录 rm 文件名 文件名 => 删除一个文件或多个文件 rm –rf 非 ...
- Java创建、重命名、删除文件和文件夹(转)
Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了.如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归. 下面是的一个解决方 ...
随机推荐
- mybatis源码阅读(动态代理)
这一篇文章主要是记录Mybatis的动态代理学习成果,如果对源码感兴趣,可以看一下上篇文章 https://www.cnblogs.com/ChoviWu/p/10118051.html 阅读本篇的 ...
- JWT认证不通过导致不能访问视图的解决方案
在做商城项目的购物车模块时,发现了一个问题. 需求:当用户登录时,添加商品到购物车的数据保存在redis.当用户未登录时,添加商品到购物车的数据保存在cookies.两个功能都写在一个视图里面.以JW ...
- java虚拟机字节码执行引擎
定义 java虚拟机字节码执行引擎是jvm最核心的组成部分之一,它做的事情很简单:输入的是字节码文件,处理过程是字节码解析的等效过程,输出的是执行结果.在不同的虚拟机实现里,执行引擎在执行java代码 ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- Django内置信号
阅读目录(Content) Django中内置的signal 自定义信号 1.定义信号 2.注册信号 3.触发信号 回到顶部(go to top) Django中内置的signal Django中提供 ...
- Gray Code——陈瑶师姐面试时候要用回溯算法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- Linux 基础——ls 命令
第二天,继续学习Linux命令... 一.查看文件和目录列表的命令 ls:显示当前目录下的文件和目录,但是不会显示隐藏的文件和目录. ls -a:显示当前目录下的所有文件和目录. ls -l:显示当前 ...
- 【转载】C++ typedef用法小结
http://www.cnblogs.com/charley_yang/archive/2010/12/15/1907384.html 第一.四个用途 用途一: 定义一种类型的别名,而不只是简单的宏替 ...
- ASP.NET MVC 视图学习,纯干货
最近用MVC专门为自己做了一个网站,用来记录文章心情和日记.加上和同事的一些交流感觉颇深.所以想把13年买的MVC 4高级编程重新看一遍,记录一些东西,以后应该用的到.视图总是被控制器渲染,因为控制器 ...
- thinkphp下实现ajax无刷新分页
1.前言 作为一名php程序员,我们开发网站主要就是为了客户从客户端进行体验,在这里,thinkphp框架自带的分页类是每次翻页都要刷新一下整个页面,这种翻页的用户体验显然是不太理想的,我们希望每次翻 ...