NSFilehandle的基本用法
来源:互联网 发布:linux如何设置ftp账户 编辑:程序博客网 时间:2024/03/29 04:59
NSFileHandle 和NSFileManager都是属于foundation框架中的类,以前一直不怎么熟悉,今天详细的了解了下。
对这些文件夹操作的时候 前提这些文件夹已经存在 否则创建的filehandle就会是nil;
+ (id)fileHandleForReadingAtPath:(NSString *)path 打开一个文件准备读取
+ (id)fileHandleForWritingAtPath:(NSString *)path 打开一个文件准备写入
+ (id)fileHandleForUpdatingAtPath:(NSString *)path 打开一个文件准备更新
- (NSData *)availableData; 从设备或通道返回可用的数据
- (NSData *)readDataToEndOfFile; 从当前的节点读取到文件的末尾
- (NSData *)readDataOfLength:(NSUInteger)length; 从当前节点开始读取指定的长度数据
- (void)writeData:(NSData *)data; 写入数据
- (unsigned long long)offsetInFile; 获取当前文件的偏移量
- (void)seekToFileOffset:(unsigned long long)offset; 跳到指定文件的偏移量
- (unsigned long long)seekToEndOfFile; 跳到文件末尾
- (void)truncateFileAtOffset:(unsigned long long)offset; 将文件的长度设为offset字节
- (void)closeFile; 关闭文件
向文件追加数据
NSString *homePath = NSHomeDirectory( );
NSString *sourcePath = [homePath stringByAppendingPathConmpone:@"testfile.text"];
NSFileHandle *fielHandle = [NSFileHandle fileHandleForUpdatingAtPath:sourcePath];
[fileHandle seekToEndOfFile]; 将节点跳到文件的末尾
NSString *str = @"追加的数据"
NSData* stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
[fileHandle writeData:stringData]; 追加写入数据
[fileHandle closeFile];
定位数据
NSFileManager *fm = [NSFileManager defaultManager];
NSString *content = @"abcdef";
[fm createFileAtPath:path contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSUInteger length = [fileHandle availabelData] length]; 获取数据长度
[fileHandle seekToFileOffset;length/2]; 偏移量文件的一半
NSData *data = [fileHandle readDataToEndOfFile];
[fileHandle closeFile];
复制文件
NSFileHandle *infile, *outfile; 输入文件、输出文件
NSData *buffer; 读取的缓冲数据
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *homePath = NSHomeDirectory( );
NSString *sourcePath = [homePath stringByAppendingPathComponent:@"testfile.txt"]; 源文件路径
NSString *outPath = [homePath stringByAppendingPathComponent:@"outfile.txt"]; 输出文件路径
BOOL sucess = [fileManager createFileAtPath:outPath contents:nil attributes:nil];
if (!success)
{
return N0;
}
infile = [NSFileHandle fileHandleForReadingAtPath:sourcePath]; 创建读取源路径文件
if (infile == nil)
{
return NO;
}
outfile = [NSFileHandle fileHandleForReadingAtPath:outPath]; 创建病打开要输出的文件
if (outfile == nil)
{
return NO;
}
[outfile truncateFileAtOffset:0]; 将输出文件的长度设为0
buffer = [infile readDataToEndOfFile]; 读取数据
[outfile writeData:buffer]; 写入输入
[infile closeFile]; 关闭写入、输入文件
[outfile closeFile];
- NSFilehandle的基本用法
- 基本文件操作:NSFileHandle的用法
- 基本文件操作:NSFileHandle的用法
- 基本文件操作:NSFileHandle的用法
- 基本文件操作:NSFileHandle的用法
- IOS NSFileHandle 基本用法
- iOS NSFileHandle 基本用法介绍
- iOS NSFileHandle 基本用法介绍
- iOS NSFileHandle 基本用法介绍
- iOS NSFileHandle 基本用法介绍
- iOS NSFileHandle 基本用法介绍
- iOS NSFileHandle 基本用法介绍
- NSFileHandle的基本使用
- [转载]NSFileHandle用法
- NSFileHandle、NSFileManager、NSUserDefaults用法
- 文件的基本操作(NSFileHandle)和文件追加例子
- IOS note (三)NSFileHandle用法
- NSFileHandle的使用
- WebLogic实用技巧——仅需2个步骤Docker在5分钟内快速运行WebLogic域
- OCILIB实例
- ubuntu下面挂载mtp设备的目录位置
- Java常用类库——Math,大数字操作类
- java基础笔试题
- NSFilehandle的基本用法
- Solr通过代码创建collection
- 整理做界面的条理
- 旋转geoserver发布的wms地图
- 【提高】沙盒目录文件解析
- PHP同时上传“多个”文件示例,并格式化$_FILES数组信息
- Android设置颜色
- ImageView设置图片的三种方法及特点
- 正则表达式 整数