UI进阶 文件管理器(NSFileManager)文件对接器(NSFileHandle)
一、文件管理器与文件连接器之间的区别
文件管理器(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)的更多相关文章
- 【原】iOS学习之文件管理器(NSFileManager)和文件对接器(NSFileHandle)
1.文件管理器(NSFileManager) 1> 主要作用及功能方法 主要作用:此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取. 功能方法: 2> 创建文件夹 创建所 ...
- 02-IOSCore - NSFileHandle、合并文件、文件指针、文件查看器
[day0201_NSFileHandle]:文件句柄 1 NSFileHandle 文件对接器.文件句柄 常用API: - (NSData *)readDataToEndOfFile;读取数据到最后 ...
- win10 uwp 打开文件管理器选择文件
本文:让文件管理器选择文件,不是从文件管理器获得文件. 假如已经获得一些文件,那么如何从文件管理器选择这些文件? 使用方法很简单. 从网上拿图来说 打开文件夹自动选择所有文件 首先需要获得文件夹,因为 ...
- 2018-8-10-win10-uwp-打开文件管理器选择文件
title author date CreateTime categories win10 uwp 打开文件管理器选择文件 lindexi 2018-08-10 19:16:50 +0800 2018 ...
- 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)
========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...
- Windows Store App JavaScript 开发:文件选取器
正如前面章节C#语言中所介绍的,文件选取器是应用与系统进行交互的一个接口,通过文件选取器可以在应用中直接与文件系统进行交互,访问不同位置的文件或文件夹,或者将文件存储在指定位置.文件选取器分为对文件进 ...
- 重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口
原文:重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (26) ...
- 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器
[源码下载] 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件保存选取器 示例1.演示如何 ...
- 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器
[源码下载] 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件打开选取器 示例1.演示如何 ...
随机推荐
- BZOJ 1123 BLO
tarjan求割点计算答案.注意不是每一棵子树都算答案.开个变量记一下. #include<iostream> #include<cstdio> #include<cst ...
- Localizing Astah – Chinese version(simplified) is now available!
Thanks to Abbey, now GUI in Astah Community can be shown in Chinese. As Abbey created Chinese one, a ...
- linux下系统启动时,几个配置文件 /etc/profile、~/.bash_profile 等几个文件的执行过程,先后顺序
1. 在登录Linux时要执行文件的过程如下: 在刚登录Linux时, 首先启动 /etc/profile 文件, 然后再启动用户目录下的 ~/.bash_profile. ~/.bash_login ...
- ora-0000 normal跟/etc/hosts有关
当hosts文件配置错误时,用sqlplus登录后startup nomount,就会报错ORA-00000 [oracle11g@testdb2 dbs]$ sqlplus "/ as s ...
- 【转】Eclipse快捷键 10个最有用的快捷键----不错
原文网址:http://www.open-open.com/bbs/view/1320934157953 1.选中你要加注释的区域,用ctrl+shift+C 会加上//注释2.先把你要注释的东西选中 ...
- python tile函数用法
tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组,我们还是使用具体的例子来说明问题 ...
- css实现鼠标经过导航文字偏位效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- FontMetrics ----- 绘制文本,获取文本高度
Canvas 绘制文本时,使用FontMetrics对象,计算位置的坐标. public static class FontMetrics { /** * The maximum distance a ...
- Free Candies
题意: 有4堆东西,每堆有n个每个有一个颜色,现在有一个篮子最多能装5个不同的颜色的东西,每次都从堆顶拿,当篮子出现两个相同颜色,可以获得这两个东西,求获得的最大数量 分析: 因为就4推,可以把各堆的 ...
- Web开发中设置快捷键来增强用户体验
从事对日外包一年多以来,发现日本的无论是WinForm项目还是Web项目都注重快捷键的使用,日本人操作的时候都喜欢用键盘而不是用鼠标去点,用他们的话来说"键盘永远比鼠标来的快",所 ...