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操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归. 下面是的一个解决方 ...
随机推荐
- jdk1.8在linux环境下的安装
转自博客:http://www.cnblogs.com/ShawnYuki/p/6816179.html
- 微信小程序实现图片上传,预览,删除
wxml: <view class='imgBox'> <image class='imgList' wx:for="{{imgs}}" wx:for-item= ...
- Kettle提高输入输出数据总结
1 mysql在数据连接是可以通过设置一下三个三处的方式 useServerPrepStmts=false useCursorFetch=true useCompression= ...
- tomcat8特性
作者:Eilen,转载需注明.博客主页:http://www.cnblogs.com/Eilen/ 一.Apache Tomcat 8介绍 Apache Tomcat 8RC1版于前几日发布.它 经过 ...
- JS判断客户端是否是iOS或者Android手机移动端
var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > - ...
- C#.Net实体代码生成工具(EntitysCodeGenerate)的使用及.NET中的ORM实现
1 引言 目前大多数项目或产品都使用关系型数据库实现业务数据的存储,这样在开发过程中,常常有一些业务逻辑需要直接用写SQL语句实现,但这样开发的结果是:遍地布满SQL语句.这些藕合较高的SQL语句给系 ...
- ueditor在QQ浏览器或者IE浏览器中无法加载
因为IE浏览器有兼容问题,打开网址,浏览器不一定以最新的文档模式加载.按F12查看 在你网址的head标签中加入:<meta http-equiv="x-ua-compatible&q ...
- javascript实现与后端相同的枚举Enum对象
; (function (global, undefined) { global.Enum = function (namesToValues) { var enumeration = functio ...
- JavaSwing 版本的简单扫雷游戏
JavaSwing 版本的简单扫雷游戏 一.扫雷游戏的基本规则 1.扫雷游戏分为初级.中级.高级和自定义四个级别. 单击游戏模式可以选择"初级"."中级".&q ...
- HDU 6186 CS Course【前后缀位运算枚举/线段树】
[前后缀枚举] #include<cstdio> #include<string> #include<cstdlib> #include<cmath> ...