一、文件管理器与文件连接器之间的区别

文件管理器(NSFileManager)

此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取。

文件连接器(NSFileHandle)

此类主要是对文件内容进行读取和写入操作。

二、文件管理器(NSFileManager)

1、创建文件夹管理器

 NSFileManager *fileManager = [NSFileManager defaultManager];

2、创建文件并写入数据

     // 创建文件夹管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
// 创建路径
NSString *homePath = NSHomeDirectory();
homePath = [homePath stringByAppendingPathComponent:@"string.txt"];
// 初始化一个字符串
7 NSString *string = @"创建一个文件并写入数据";
// 创建文件string.txt 并向stirng.txt中添加数据
// 参数1:文件路径
// 参数2:NSData类型
// 参数3:参数
BOOL result = [fileManager createFileAtPath:homePath contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
if (result) {
NSLog(@"成功 %@", homePath);
} else {
NSLog(@"失败");
}

3、从一个文件中读取数据

     // 创建文件夹管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
// 创建路径
NSString *homePath = NSHomeDirectory();
homePath = [homePath stringByAppendingPathComponent:@"string.txt"];
// 从string.txt中获取数据
NSData *data = [fileManager contentsAtPath:homePath];
// 将NSData类型的数据转换成NSString类型的数据,并输出
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string %@", string);

4、文件的移动

     // 创建文件夹管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
// 创建路径
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"string.txt"];
// 创建toPath
NSString *toPath = [homePath stringByAppendingPathComponent:@"test.txt"];
// 将string.txt中的数据移动到test.txt中,并将string.txt删除
BOOL result = [fileManager moveItemAtPath:filePath toPath:toPath error:nil];
if (result) {
NSLog(@"成功 %@", toPath);
} else {
NSLog(@"失败");
}

5、文件的复制

     // 创建文件夹管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
// 创建路径
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test.txt"];
// 创建toPath
NSString *toPath = [homePath stringByAppendingPathComponent:@"copy.txt"];
// 将test.txt中的数据复制到copy.txt中,保留test.txt文件
BOOL result = [fileManager copyItemAtPath:filePath toPath:toPath error:nil];
if (result) {
NSLog(@"成功 %@", toPath);
} else {
NSLog(@"失败");
}

6、比较两个文件的内容是否一样

     // 创建文件夹管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
// 创建路径
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test.txt"];
// 创建toPath
NSString *toPath = [homePath stringByAppendingPathComponent:@"copy.txt"];
// 比较两个文件的内容是否一致
BOOL result = [fileManager contentsEqualAtPath:filePath andPath:toPath];
if (result) {
NSLog(@"内容一致");
} else {
NSLog(@"内容不一致");
}

7、判断文件是否存在

     // 创建文件夹管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
// 创建路径
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test.txt"];
// 判断文件是否存在
BOOL result = [fileManager fileExistsAtPath:filePath];
if (result) {
NSLog(@"文件存在");
} else {
NSLog(@"文件不存在");
}

8、移除文件

     // 创建文件夹管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
// 创建路径
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test.txt"];
// 移除文件
BOOL result = [fileManager removeItemAtPath:filePath error:nil];
if (result) {
NSLog(@"移除成功");
} else {
NSLog(@"移除失败");
}

9、创建文件夹

1、根据给定的文件夹路径创建

     // 创建文件夹管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
// 创建路径
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test1/test2"];
// 创建文件夹
BOOL result = [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
if (result) {
NSLog(@"创建成功");
} else {
NSLog(@"创建失败");
}

2、根据给定的文件路径,创建其所在文件夹

     // 创建文件夹管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
// 创建路径
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"test3/test4/hello.txt"];
// 创建文件夹test4
// stringByDeletingLastPathComponent 删除路径最后一个/以及后面的内容
BOOL result = [fileManager createDirectoryAtPath:[filePath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
if (result) {
NSLog(@"创建成功");
} else {
NSLog(@"创建失败");
}

三、文件连接器(NSFileHandle)

NSFileHandle是非常基础的只针对文件内容的操作(写入,读取,更新),是把NSData通过连接器一个字节一个字节的写入/读取文件.(NSData <—> NSFileHandle <—> 文件).

使用场景:
    对文件内容的进行局部修改、追加内容。

使用步骤
    1).文件对接并获取一个NSFileHandle对象.
    2).读写操作
    3).关闭对接

注意:NSFileHandle类并没有提供创建文件的功能。必须使用NSFileManager方法来创建文件。因此,在使用下表中的方法时,都是保证文件已经存在,否则返回nil.

1、读取数据

1、读取文件中的全部数据

     // string.txt已经存在,获取其路径
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
// 创建文件连接器,并打开一个文件准备读取
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
// 从string.txt中读取全部数据
NSData *data = [handle availableData];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string %@", string);
// 关闭文件连接器
[handle closeFile];

2、从指定位置读取到文件末尾

注意,当文件中的数据是汉字时,因为utf-8的中文字符是占三个字节,所以偏移量必须是3的倍数,否则读取不到数据

     // string.txt已经存在,获取其路径
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
// 创建文件连接器,并打开一个文件准备读取
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
// 获取stirng.txt中数据字节数
NSUInteger length = [[handle availableData] length];
// 偏移量为文件的一半
[handle seekToFileOffset:length/2.0];
// 获取当前文件的偏移量
NSLog(@"%llu", [handle offsetInFile]);
// 从数据的一半位置开始读取,读取到文件末尾
NSData *data = [handle readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string %@", string);
[handle closeFile];

3、从指定位置读取指定长度的数据

     // string.txt已经存在,获取其路径
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
// 创建文件连接器,并打开一个文件准备读取
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
// 获取stirng.txt中数据长度
NSUInteger length = [[handle availableData] length];
// 偏移量为文件的一半
[handle seekToFileOffset:length/2.0];
// 从数据的一半位置开始读取,读取3个字节
NSData *data = [handle readDataOfLength:];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string %@", string);
[handle closeFile];

2、截取文件为指定长度

     // string.txt已经存在,获取其路径
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
// 创建文件连接器,并打开一个文件写入读取
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// 将文件的长度设置为21字节
[handle truncateFileAtOffset:];
[handle closeFile];

3、在文件的指定位置拼接数据

     // string.txt已经存在,获取其路径
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
// 创建文件连接器,并打开一个文件写入(或更新)读取
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// 搜索到文件内容末尾
[handle seekToEndOfFile];
NSString *appendStr = @" world";
// 在文件的末尾拼接字符串
[handle writeData:[appendStr dataUsingEncoding:NSUTF8StringEncoding]];
[handle closeFile];

UI进阶 文件管理器(NSFileManager)文件对接器(NSFileHandle)的更多相关文章

  1. 【原】iOS学习之文件管理器(NSFileManager)和文件对接器(NSFileHandle)

    1.文件管理器(NSFileManager) 1> 主要作用及功能方法 主要作用:此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取. 功能方法: 2> 创建文件夹 创建所 ...

  2. 02-IOSCore - NSFileHandle、合并文件、文件指针、文件查看器

    [day0201_NSFileHandle]:文件句柄 1 NSFileHandle 文件对接器.文件句柄 常用API: - (NSData *)readDataToEndOfFile;读取数据到最后 ...

  3. win10 uwp 打开文件管理器选择文件

    本文:让文件管理器选择文件,不是从文件管理器获得文件. 假如已经获得一些文件,那么如何从文件管理器选择这些文件? 使用方法很简单. 从网上拿图来说 打开文件夹自动选择所有文件 首先需要获得文件夹,因为 ...

  4. 2018-8-10-win10-uwp-打开文件管理器选择文件

    title author date CreateTime categories win10 uwp 打开文件管理器选择文件 lindexi 2018-08-10 19:16:50 +0800 2018 ...

  5. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  6. Windows Store App JavaScript 开发:文件选取器

    正如前面章节C#语言中所介绍的,文件选取器是应用与系统进行交互的一个接口,通过文件选取器可以在应用中直接与文件系统进行交互,访问不同位置的文件或文件夹,或者将文件存储在指定位置.文件选取器分为对文件进 ...

  7. 重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口

    原文:重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (26) ...

  8. 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器

    [源码下载] 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件保存选取器 示例1.演示如何 ...

  9. 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器

    [源码下载] 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件打开选取器 示例1.演示如何 ...

随机推荐

  1. toB的产品经理和toc产品经理区别

    腾讯产品经理现身说法 曾经在UC做过2年to c的app,现在在腾讯做to b的产品. 做to c产品的时候,我很瞧不起做to b产品的同学,认为他们不过是做支撑的. 后来,我参与了一个to b平台级 ...

  2. 修改Android系统字号(一)

    /*********************************************************************** * 修改Android系统字号(一) * 说明: * ...

  3. 解决android sdk manage打开闪退的解决方法

    在打开android sdk mangage.exe的时候,一闪而过,在eclipse中出现如下提示: [2015-07-20 13:42:23 - SDK Manager] [SDK Manager ...

  4. UVa120 - Stacks of Flapjacks

    Time limit: 3.000 seconds限时:3.000秒 Background背景 Stacks and Queues are often considered the bread and ...

  5. Android的IPC机制(一)——AIDL的使用

    综述 IPC(interprocess communication)是指进程间通信,也就是在两个进程间进行数据交互.不同的操作系统都有他们自己的一套IPC机制.例如在Linux操作系统中可以通过管道. ...

  6. 【Sass初级】开始使用Sass和Compass

    转自:http://www.w3cplus.com/preprocessor/beginner/getting-started-with-sass-and-compass.html 如果你的朋友.同事 ...

  7. python tile函数用法

    tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组,我们还是使用具体的例子来说明问题 ...

  8. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.5.安装Grid,创建ASM磁盘组空间不足

    因之前分区时,分区的Last cylinder的值选了“1”,导致创建磁盘组空间不足.解决办法是先删除分区,重新创建分区并删除ASM磁盘,然后重建ASM磁盘 1. 先删除分区,重新创建分区: 1)查询 ...

  9. android 带边框的圆角按钮

    新建buttonstyle.xml 代码如下 <?xml version="1.0" encoding="UTF-8"?> <layer-li ...

  10. Yii系列教程(四):使用Memcache保存会话

    1环境准备 安装Memcached服务端: yum -y installmemcached.x86_64 安装PHP-Memcache扩展: yum -y installphp-pecl-memcac ...