1. 沙盒 & NSData

/*_______________________________获取沙盒路径_________________________________________*/

//第一种获取方式

//NSHomeDirectory();获取到沙盒的目录路径

NSString *homePath = NSHomeDirectory();

NSLog(@"沙盒目录:%@",homePath);

NSString *docPath1 = [NSString stringWithFormat:@"%@/Documents",homePath];

NSString *docPath2 = [homePath stringByAppendingString:@"/Documents"];

NSLog(@"\ndocPath1:%@,\ndocPath2:%@",docPath1,docPath2);

//第二种获取方式.

/*

NSDocumentDicrectory: Documents文件夹

NSLibraryDirectory  : Library文件夹

*/

/*

NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSLog(@"array:%@",array);

2. 沙盒 & NSData & NSFileManager

(NSFileManager:文件的创建、复制、删除、前切文件(注意:操作对象为文件,而不是文件内容)。)

//演示路径

NSString *path = @"/Users/apple/file.text";

//1.返回路径的组成部分

NSArray *array = [path pathComponents];

NSLog(@"pathComponents:%@",array);

//2.路径的最后组成部分

NSString *lastPathComponent = [path lastPathComponent];

NSLog(@"lastComponent:%@",lastPathComponent);

//3.追加子路径

NSString *newPath1 = [path lastPathComponent];

NSLog(@"newPath1=%@",newPath1);

NSString *newPath2 = [path stringByAppendingPathComponent:@"/appFile.text"];

NSLog(@"newPath2:%@",newPath2);

//4.删除最后的组成部分

NSString *deleteLast = [path stringByDeletingLastPathComponent];

NSLog(@"deleteLast:%@",deleteLast);

//5.删除扩展名

NSString *deleteExtension = [path stringByDeletingPathExtension];

NSLog(@"deleteExtension:%@",deleteExtension);

//6.获取路径最后组成部分的扩展名

NSString *extension =[path pathExtension];

NSLog(@"extension:%@",extension);

//7.追加扩展名

NSString *appendExt = [path stringByAppendingPathExtension:@"jpg"];

NSLog(@"appendExt:%@",appendExt);

//NSString->NSData

NSString *s = @"tsdfsdfsdfsdf";

NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];

//NSData->NSString

NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"str:%@",str);

//NSMutableData 可变的Data对象,可以追加数据

3. 沙盒 & NSData & NSFileManager

(NSFileManger:文件的创建、复制、删除、剪切文件(注意:操作对象为文件,而不是文件内容))

/*___________________________________1.创建文件_____________________________________*/

/*

//获取当前app的沙盒目录

NSString *homePath = NSHomeDirectory();

//追加子路径

NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];

//NSFileManager不能使用alloc创建,这个类设计为单例

NSFileManager *fileManager1 = [NSFileManager defaultManager];

NSFileManager *fileManager2 = [NSFileManager defaultManager];

NSLog(@"f1=%p,f2=%p",fileManager1,fileManager2);  //观察地址,发现相同

//NSFileManager只能通过类方法defaultManager创建

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *string = @"自动化测试部";

//将NSString 转化为 NSData对象.

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

//根据路径filePath创建对应的文件,注意:只能创建文件,不能创建目录(文件夹).

BOOL success = [fileManager createFileAtPath:filePath contents:data attributes:nil];

if (success) {

NSLog(@"文件创建成功");

}else {

NSLog(@"文件创建失败");

}

//创建文件夹

NSString *filePath2 = [homePath stringByAppendingPathComponent:@"Documents/demo"];

NSError *error;

BOOL success2 = [fileManager createDirectoryAtPath:filePath2 withIntermediateDirectories:YES attributes:nil error:&error];

if (!success2) {

NSLog(@"创建失败:%@",error);

}

*/

/*___________________________________2.读取文件_____________________________________*/

/*

//获取当前app的沙盒根目录

NSString *homePath = NSHomeDirectory();

//追加子路径

NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];

NSFileManager *fileManager = [NSFileManager defaultManager];

//根据路径读取文件中的数据

NSData *data = [fileManager contentsAtPath:filePath];

//NSData 转 NSString

NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@",string);

*/

/*___________________________________3.移动(前切)文件_____________________________________*/

/*

//获取当前app的沙盒根目录

NSString *homePath = NSHomeDirectory();

//源路径

NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];

//目标路径

NSString *targetPath = [homePath stringByAppendingPathComponent:@"Documents/demo/file2.text"];

NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL success = [fileManager moveItemAtPath:filePath toPath:targetPath error:nil];

if (!success) {

NSLog(@"移动失败");

}

//一个思考:怎么实现给移动至此的文件改名字?? Method:通过剪切,文件从当前目录移动至当前目录

*/

/*___________________________________4.复制文件_____________________________________*/

/*

//获取当前app的沙盒目录

NSString *hoemPath = NSHomeDirectory();

//源路径

NSString *filePath = [hoemPath stringByAppendingPathComponent:@"Documents/demo/file3.text"];

//目标路径

NSString *targetPath = [hoemPath stringByAppendingPathComponent:@"Documents/file.text"];

NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL success = [fileManager copyItemAtPath:filePath toPath:targetPath error:nil];

if (!success) {

NSLog(@"复制失败");

}

*/

/*___________________________________3.删除文件_____________________________________*/

/*

//获取当前app的沙盒目录

NSString *hoemPath = NSHomeDirectory();

//源路径

NSString *filePath = [hoemPath stringByAppendingPathComponent:@"Documents/demo/file3.text"];

NSFileManager *fileManager = [NSFileManager defaultManager];

//判断文件是否存在

BOOL fileExist = [fileManager fileExistsAtPath:filePath];

if (fileExist) {

//removeItemAtPath:删除文件.

BOOL success = [fileManager removeItemAtPath:filePath error:nil];

if (success) {

NSLog(@"删除成功");

}

*/

/*___________________________________4.获取文件的属性_____________________________________*/

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *homePath = NSHomeDirectory();

//目标路径

NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];

//获取到文件的属性

NSDictionary *fileAttr = [fileManager attributesOfItemAtPath:filePath error:nil];

NSLog(@"%@",fileAttr);

NSNumber *fileSize = [fileAttr objectForKey:NSFileSize];

long sizeValue = [fileSize longValue];

NSLog(@"文件大小:%ld",sizeValue);

//如下读取文件的大小不可取,因为将文件中的数据全都读到内存中,文件大时,太占内存了。

//NSData *data = [fileManager contentsAtPath:filePath];

//NSInteger len = data.length;

4. 读写文件 & NSFileHandle -文件内容操作

(Attention:NSFileHandle区别NSFileManager(主要对目录操作),NSFileHandle(主要对文件内容操作))

/*_______________________________NSString读、写文件__________________________________________*/

/*

//1.NSString写文件

NSString *s = @"自动化测试部门";

//文件路径

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/filetext"];

//将字符串写入文件

BOOL success = [s writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

if (success) {

NSLog(@"字符串写入成功");

}

//2.NSString读文件

//创建字符串时同时读取文件路径对应的文件中的内容

NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

NSLog(@"string=%@",string);

*/

/*_______________________________NSData读、写文件__________________________________________*/

/*

//1.NSData读文件

//文件路径

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/file.text"];

//创建NSData时,同时读取文件中的内容

NSData *data = [[NSData alloc] initWithContentsOfFile:path];

NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@",string);

//2.NSData写文件

//[data writeToFile:<#(nonnull NSString *)#> atomically:<#(BOOL)#>]

*/

/*

注意:NSArray、NSDictionary 中只能存放NSNumber、NSString、NSData、NSDate、NSArray、NSDictionary

才能成功写入文件、写入文件我们成之为“属性列表文件”

*/

/*_______________________________NSArray读、写文件__________________________________________*/

//1.NSArray写文件

/*

NSString *s1 = @"zhangsan";

NSString *s2 = @"李四";

//文件路径

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/array.plist"];

NSLog(@"path:%@",path);

NSArray *array = [[NSArray alloc] initWithObjects:s1,s2, nil];

BOOL success = [array writeToFile:path atomically:YES];

if (success) {

NSLog(@"写入成功");

}

*/

//数组、字典存入能存入NSNumber、NSData、NSDate、NSArray、NSDictionary以外的对象,则无法写入文件

/*

Person *p = [[Person alloc] init];

NSArray *array2 = [NSArray arrayWithObjects:p,@"demo", nil];

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/array2.plist"];

BOOL success2 = [array2 writeToFile:path atomically:YES];

if (!success2) {

NSLog(@"写入失败");

}

*/

/*

//2.NSArray读文件

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/array.plist"];

//        NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Property List.plist"];

NSArray *readArray = [[NSArray alloc] initWithContentsOfFile:path];

for (NSString *s in readArray) {

NSLog(@"s = %@",s);

}

*/

/*_______________________________NSDictionary读、写文件__________________________________________*/

//1.NSDictionary写入文件

NSDictionary *dic = @{

@"name":@"jack",

@"birthday":[NSDate date],

@"age":@22

};

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/dic.plist"];

BOOL success = [dic writeToFile:path atomically:YES];

if (success) {

NSLog(@"写入成功");

}

//2.NSDictionary读文件

NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:path];

NSLog(@"readDic:%@",readDic);

5. 追加数据与定位读取

/*_____________________________________1.追加数据_____________________________________*/

/*

NSString *s = @"MLB-AE-SW";

//当前登陆用户的主目录

NSString *homePath = NSHomeDirectory();

NSString *path = [homePath stringByAppendingPathComponent:@"file.text"];

//写入文件

[s writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

*/

/*

NSString *homePath = NSHomeDirectory();

NSString *path = [homePath stringByAppendingPathComponent:@"file.text"];

//创建一个写入的NSFileHandle对象

NSFileHandle *writeHandle = [NSFileHandle fileHandleForWritingAtPath:path];

//将文件的偏移量设置到末尾,写入文件时则从末尾开始写入

[writeHandle seekToEndOfFile];

NSString *appendString = @"追加的数据";

NSData *data = [appendString dataUsingEncoding:NSUTF8StringEncoding];

//从当前偏移量开始写入数据

[writeHandle writeData:data];

//关闭文件

[writeHandle closeFile];

*/

/*_____________________________________2.定位读取_____________________________________*/

//当前登陆用户主目录

NSString *homePath = NSHomeDirectory();

NSString *path = [homePath stringByAppendingPathComponent:@"Documents/file.text"];

//通过NSFileManager获取文件大小

NSFileManager *fileManager = [NSFileManager defaultManager];

NSDictionary *fileAttr = [fileManager attributesOfItemAtPath:path error:nil];

NSNumber *fileSize = [fileAttr objectForKey:NSFileSize];

long long sizeValue = [fileSize longLongValue];

NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];

//将偏移量设置到中间位置

[readHandle seekToFileOffset:sizeValue/2];

//从当前偏移量读取到文件的末尾

NSData *data = [readHandle readDataToEndOfFile];

//NSData ---> NSString

NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@",string);

6. NSFileHandle实现复制文件的功能

NSString *homePath = NSHomeDirectory();

NSString *srcPath = [homePath stringByAppendingPathComponent:@"06 第六课 文件管理.pdf"];

//目标文件路径

NSString *targetPath = [homePath stringByAppendingPathComponent:@"Documents/06 第六课 文件管理.pdf"];

/*

注意:使用NSFileHandle只能读写译经存在的文件,不能创建文件

使用NSFileManager创建文件

*/

NSFileManager *fileManager = [NSFileManager defaultManager];

//创建目标文件

BOOL success = [fileManager createFileAtPath:targetPath contents:nil attributes:nil];

if (success) {

NSLog(@"目标文件创建成功!");

}

//创建用于读取文件的NSFileHandle对象

NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:srcPath];

//创建用于写入的NSFileHandle对象

NSFileHandle *writerHande = [NSFileHandle fileHandleForWritingAtPath:targetPath];

//从当前偏移量读到文件的末尾,偏移量默认是起始位置

//NSData *data = [readHandle readDataToEndOfFile];

//同上

NSData *data = [readHandle availableData];  //缺陷:每次读取文件全部内容(当文件很大时,程序就会crash)

//解决方式:一次读几个字节,一段一段的读.

//将数据写入目标文件

[writerHande writeData:data];

//关闭文件

[readHandle closeFile];

[writerHande closeFile];

iOS - 文件操作(File Operating)的更多相关文章

  1. IOS文件操作的两种方式:NSFileManager操作和流操作

    1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...

  2. JAVASE02-Unit06: 文件操作——File 、 文件操作—— RandomAccessFile

    Unit06: 文件操作--File . 文件操作-- RandomAccessFile java.io.FileFile的每一个实例是用来表示文件系统中的一个文件或目录 package day06; ...

  3. iOS——文件操作NSFileManager (创建、删除,复制,粘贴)

    iOS——文件操作NSFileManager (创建.删除,复制,粘贴)       iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...

  4. c#中@标志的作用 C#通过序列化实现深表复制 细说并发编程-TPL 大数据量下DataTable To List效率对比 【转载】C#工具类:实现文件操作File的工具类 异步多线程 Async .net 多线程 Thread ThreadPool Task .Net 反射学习

    c#中@标志的作用   参考微软官方文档-特殊字符@,地址 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/toke ...

  5. python的文件操作file:(内置函数,如seek、truncate函数)

    file打开文件有两种方式,函数用file()或者open().打开后读入文件的内容用read()函数,其读入是从文件当前指针位置开始,所以需要控制指针位置用: 一.先介绍下file读入的控制函数: ...

  6. 【转载】C#工具类:实现文件操作File的工具类

    在应用程序的开发中,文件操作的使用基本上是必不可少的,FileStream类.StreamWriter类.Directory类.DirectoryInfo类等都是文件操作中时常涉及到的类,我们可以通过 ...

  7. PythonStudy——文件操作 File operation

    # 文件:就是硬盘的一块存储空间 # 1.使用文件的三步骤: # 打开文件- 得到文件对象:找到数据存放在硬盘的位置,让操作系统持有该空间,具有操作权# 硬盘空间 被 操作系统持有# 文件对象f 被 ...

  8. 文件操作(FILE)与常用文件操作函数

    文件 1.文件基本概念 C程序把文件分为ASCII文件和二进制文件,ASCII文件又称文本文件,二进制文件和文本文件(也称ASCII码文件)二进制文件中,数值型数据是以二进制形式存储的, 而在文本文件 ...

  9. Java 文件操作-File

    1.File文件操作 java.io.File用于表示文件(目录),也就是说程序员可以通过File类在程序中操作硬盘上的文件和目录.File类只用于表示文件(目录)的信息(名称.大小等),不能对文件的 ...

随机推荐

  1. Meta键盘

    由于著名的编辑器Emacs中用到Meta键,但如今大多国人所用键盘上实际并无此键,想必多有不明之处,故多方收集资料撰写此文,简要描述了Meta键及相关键盘的发展始末,至于在Emacs上如何使用国人键盘 ...

  2. iOS开发笔记系列-基础5(分类和协议)

    分类 在Objective-C中,除了通过新建子类的方式来向类添加新方法外,还可以通过分类的方式.分类提供了一种简单的方式,将类的定义模块化到相关方法的组或分类中,它还提供了扩展现有类定义的简便方式, ...

  3. 一个不错的JavaScript解析浏览器路径方法(转)

    JavaScript中有时需要用到当前的请求路径等涉及到url的情况,正常情况下我们可以使用location对象来获取我们需要的信息,本文从另外一个途径来解决这个问题,而且更加巧妙 方法如下: fun ...

  4. hdu 4099 Revenge of Fibonacci 字典树+大数

    将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...

  5. ERROR 1227 (42000): Access denied; you need (at least one of) the PROCESS privilege(s) for this oper

    1  用以往的mysql登陆模式登陆 [mysql@eanintmydbc002db1 mysqllog]$ mysql Enter password:  Welcome to the MySQL m ...

  6. win7中USB音箱没有声音解决的方法

    Win7装好后,原来在XP中工作正常的USB小音箱却不工作了,重装了声卡驱动还是一样,后来通过下面尝试最终好用了. 1.右键右下角喇叭button. 2.点击"播放设备". 3.设 ...

  7. yii中阻止 SHOW CREATE TABLE and SHOW COLUMNS 每次执行

    Disable SHOW CREATE TABLE and SHOW COLUMNS in Yii为啥会显示:SHOW CREATE TABLE and SHOW COLUMNS 答案:This is ...

  8. Centos 安装KScope1.6.2

    准备工作:安装ctags graphviz,和cscope  (可以用yum install来安装) 1.首先下载kscope,最好下载16.x的版本,这个最好的 下载kscope-1.6.2.tar ...

  9. C# 之 将string数组转换到int数组并获取最大最小值

    1.string 数组转换到 int 数组 " }; int[] output = Array.ConvertAll<string, int>(input, delegate(s ...

  10. 使用JS制作一个鼠标可拖的DIV(四)——缩放

    原理与鼠标拖动 DIV 相同. 下面就先实现一个在DIV的右上角显示一个小正方形(类似). 当鼠标按下并拖动时,DIV会以要拖动的元素的左下角的坐标点定位,根据鼠标的拖动,按比例的扩大或缩小. 一.思 ...