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

文件管理器(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. Java 图片转换为字符图 CharMaps (整理)

      /* * Java 图片转换成字符图 CharMaps (整理) * * 2016-1-2 深圳 南山平山村 曾剑锋 * * @(#)CharMaps.java 2014/1/16 * 1.这个一 ...

  2. 20160202.CCPP体系详解(0012天)

    内容概要:C语言控制语句题库.doc 第三章 控制语句 一.选择题 1. 以下语句中无限循环语句是[B]. A)for(;2&5;); B)while(1,2,3); -> while( ...

  3. python练习程序(c100经典例10)

    题目: 打印楼梯,同时在楼梯上方打印两个笑脸. print '..' for i in range(1,9): print '**' for j in range(1,i+1): print ' ',

  4. HDU5427

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> us ...

  5. 嵌入式 VFS: Cannot open root device "mtdblock2" or unknown-block(2,0)

    系统启动后,虽然nand驱动表现正常,但是最后挂载rootfs时候出错: Kernel command line: root=/dev/mtdblock2 rw init=/linuxrc conso ...

  6. gcc-4.8.3安装,gdb-7.6安装

    gdb用法: http://blog.chinaunix.net/uid-26548237-id-3435525.html gdb-7.6.tar.gz:  (官网下载:http://ftp.gnu. ...

  7. [转]linux之date命令

    转自:http://www.cnblogs.com/peida/archive/2012/12/13/2815687.html 在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用 ...

  8. JS判断是不是本页面并且,给标签添加属性和属性值

    大多是在导航栏中用到的,在导航栏中在主页和在其他的网页点击导航栏中的主页是不同的,主要就是判断这个. 我是在ascs页面中写的. 下面先看标签: <a href="http://www ...

  9. 进入appstore中指定的应用

    1.进入appstore中指定的应用 NSString *str = [NSString stringWithFormat:                           @"itms ...

  10. python的元组和列表使用之一

    Python的列表和元组 1.       概述 列表是用方括号[]包围的数据集合,不同的成员之间用逗号进行分隔,列表可以通过序号来进行访问其中的成员,可以对列表进行排序.添加.删除操作,改变列表中某 ...