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操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归. 下面是的一个解决方 ...
随机推荐
- Android IPC
1. 什么是Android IPC IPC:inter-process Commnication跨进程的通信,多进程之间的通信,不同的操作系统有不同的通信方式,Android继承自Linux,但其IP ...
- [New learn] NSOperation基本使用
1.简介 NS(基于OC语言)是对GCD(基于C语言)的封装,让开发者能够更加友好的方便的去使用多线程技术. 2.NSOperation的基本使用 NSOperation是抽象类,所以如果要使用NSO ...
- 让我们来一起学习OC吧
在本分类中的接下来的将翻译http://rypress.com/tutorials/objective-c/index 通过每一章节的翻译,使得自己的OC基础扎实并分享给大家.
- vue-cli脚手架引入element UI的正确打开方式
element UI官网教程:http://element-cn.eleme.io/#/zh-CN/component/quickstart 1.完整引入,直接了当,但是组件文件不是按需加载,造成多余 ...
- eetcode 之String to Integer (atoi)(28)
字符串转为数字,细节题.要考虑空格.正负号,当转化的数字超过最大或最小是怎么办. int atoi(char *str) { int len = strlen(str); ; ; ; while (s ...
- linux命令(45):diff命令
1.命令格式: diff[参数][文件1或目录1][文件2或目录2] 2.命令功能: diff命令能比较单个文件或者目录内容.如果指定比较的是文件,则只有当输入为文本文件时才有效.以逐行的方式,比较文 ...
- maven新建web项目提示The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
maven新建web项目提示The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bu ...
- Longest Valid Parentheses——仍然需要认真看看(动态规划)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Centos7Yum安装配置指定版本nginx
1.安装 rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm 2.启 ...
- 【JBPM4】查询流程实例当前所在节点
示例代码: ProcessEngine processEngine = Configuration.getProcessEngine(); ExecutionService executionServ ...