- (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. SAE如何使用Git

    了解Git及远程git仓库 请先看博文<Git入门及上传项目到github中>,弄懂了之后我相信我下面说的就相当于废话了. SAE的git远程仓库就相当于github. 向SAE的远程仓库 ...

  2. 查看linux 下进程运行时间(转)

    原文地址:http://blog.csdn.net/tspangle/article/details/11731317 可通过ps 来查看,通过参数 -o 来查看 如: ps -eo pid,tty, ...

  3. 3.22. grep sed re

    1.整理正则表达式博客 re  http://www.cnblogs.com/oyoui/p/6599846.html 2.grep(正则表达式及字符处理) 1.显示出所有含有root的行: [roo ...

  4. linux命令(24):find命令

    1.命令格式: find pathname -options [-print -exec -ok ...] 2.命令功能: 用于在文件树种查找文件,并作出相应的处理 3.命令参数: pathname: ...

  5. hihocoder 1178 : 计数

    #1178 : 计数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Rowdark是一个邪恶的魔法师.在他阅读大巫术师Lich的传记时,他发现一类黑魔法来召唤远古生物, ...

  6. Genymotion的2个问题及解决方法

    问题一:Unable to connect to your virtual device 解决方法:基本都是内存不够导致的,在virtualbox中调整对应虚拟机的内存,确保任务管理器中的性能一项中剩 ...

  7. LOJ #6277. 数列分块入门 1-分块(区间加法、单点查询)

    #6277. 数列分块入门 1 内存限制:256 MiB时间限制:100 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 2   题目描述 给出 ...

  8. Java中的冒泡排序(减少比较次数)

    package yzhou.sort; import java.util.Arrays; public class BubbleSort { public static void main(Strin ...

  9. js前端分享功能

    直接添加网页分享代码就可以了.实现方法如下:1.打开百度分享:http://share.baidu.com.2.点击“代码获取”.3.进行个性化设置.

  10. UGUI的优点新UI系统二 直观、易于使用

    UGUI的优点新UI系统二 直观.易于使用   对于UI控件,开发者可以直接使用鼠标在Scene视图里编辑它们的大小.位置和旋转角度,而无需编写任何代码,以Button为例,如图1-3.图1-4和图1 ...