沙盒、文件以及文件夹的操作
来源:互联网 发布:珠宝设计软件下载 编辑:程序博客网 时间:2024/05/21 06:46
NSData
// 把NSString转化成NSData NSData *dataContents = [str dataUsingEncoding:NSUTF8StringEncoding];
沙盒
//沙盒路径 //方法一(主目录文件) NSString *sandBoxPath = NSHomeDirectory(); NSLog(@"%@", sandBoxPath); //方法二 (iOS开发mac开发均可用) NSArray *sandBoxPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //拼接方法一(拼接前加/) NSString *path1 = [sandBoxPath stringByAppendingString:@"/Documents"]; NSLog(@"%@", path1); //拼接方法二(不用加/) NSString *path2 = [sandBoxPath stringByAppendingPathComponent:@"file.txt"]; [[NSFileManager defaultManager] createFileAtPath:path2 contents:nil attributes:nil]; NSLog(@"path2:%@", path2);
文件以及文件夹操作
NSFileManager *fileManeger = [NSFileManager defaultManager]; NSString *str = @"hello world"; NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; NSString *homePath = NSHomeDirectory(); //创建文件 NSString *filePath = [homePath stringByAppendingPathComponent:@"Desktop/file.txt"]; [fileManeger createFileAtPath:filePath contents:data attributes:nil]; //文件夹的创建 NSString *dirPath = [homePath stringByAppendingPathComponent:@"/Desktop/Apel0811"]; [fileManeger createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil]; //读取文件 NSData *fileData = [fileManager contentsAtPath:filePath2]; NSString *string = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding]); //复制文件 //目标地址必须要加上文件地址 [fileManeger copyItemAtPath:srcPath toPath:dstPath error:nil]; //剪切文件 //目标地址必须要加上文件地址 [fileManeger moveItemAtPath:srcPath toPath:dstPath error:nil]; //删除文件 [fileManeger removeItemAtPath:homePath error:nil];
文件内容操作
//写入 //写入文件内容,如果没有就会添加 NSString *str = @"hello world"; NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/string.txt"]; [str writeToFile:path1 atomically:NO encoding:NSUTF8StringEncoding error:nil]; //写入NSDictionary NSDictionary *dic = @{ @"key1" : @"value1", @"key2" : @"value2", @"key3" : @"value3" }; NSString *path2 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/dictionary.plist"]; [dic writeToFile:path2 atomically:YES]; //写入NSArray NSArray *array = @[@1, @2, @3]; NSString *path3 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/array.plist"]; [array writeToFile:path3 atomically:YES]; //读取 //读取文本文件 NSString *readStr = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil]; //读取数组文件 NSArray *readArray = [NSArray arrayWithContentsOfFile:path3]; //读取字典文件 NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:path2];
0 0
- 沙盒、文件以及文件夹的操作
- 文件以及文件夹的操作类
- Python 对文件夹以及文件的操作
- C#操作文件以及文件夹
- C#中对文件以及文件夹的操作
- 【学习Linux】文件以及文件夹的基本操作
- 文件、文件夹的操作
- 文件、文件夹的操作
- MFC 中打开 文件,文件夹,以及删除文件,文件夹操作
- iOS 文件以及文件夹相关操作
- 沙盒文件以及文件夹操作
- MFC 对文件以及文件夹操作
- 文件操作以及实现文件的复制操作并且在目的目录创建相应文件夹
- 文件以及文件夹的复制
- Android 初始文件夹创建 以及 文件&文件夹相关操作
- 文件和文件夹的操作
- C# 文件,文件夹的操作
- 文件和文件夹的操作
- 课堂笔记(2)
- 机房重构之报表
- Mac OS X下搭建NDK环境
- AJAX 是什么?
- for循环实现各种图形输出
- 沙盒、文件以及文件夹的操作
- Spring 源码分析
- ios拼图游戏(四)之触摸交换拼图
- 集合
- Unity3D在Project中添加Favorites快速搜索标签
- 深入理解Java内存模型——volatile
- poj 2975
- Dinic算法的原理与构造(转)
- HDU 5676 ztr loves lucky numbers【DFS】