1、NSFileHandle是oc中用来操作文件的类,通过此类可以读写文件,可以断点续传。
2、NSFileHandle类中常用的方法
//文件中的全部内容(有效数据,包括空格和汉字,其中空格在一个字节,汉字占三个字节)
@property (readonly, copy) NSData *availableData;
//读取文件中的全部内容
- (NSData *)readDataToEndOfFile;
//读取文件中指定长度(字节)的内容
- (NSData *)readDataOfLength:(NSUInteger)length;
//写数据到文件中
- (void)writeData:(NSData *)data;
//文件末尾的偏移量
- (unsigned long long)seekToEndOfFile;
//跳到文件中指定位置的偏移量
- (void)seekToFileOffset:(unsigned long long)offset;
//将文件的长度设定为指定长度的字节
- (void)truncateFileAtOffset:(unsigned long long)offset;
//关闭文件
- (void)closeFile;
//打开一个文件准备读取
+ (instancetype)fileHandleForReadingAtPath:(NSString *)path;
//打开一个文件准备写入
+ (instancetype)fileHandleForWritingAtPath:(NSString *)path;
//打开一个文件准备更新(写入、更新)
+ (instancetype)fileHandleForUpdatingAtPath:(NSString *)path;
3、例子
//
// main.m
// NSFileHandleStudy
//
// Created by 肖国健 on 15-3-4.
// Copyright (c) 2015年 Shone. All rights reserved.
//
#import <Foundation/Foundation.h>
//读取文件中的内容
void readFileContent(){
//获取根路径 /Users/xiaoguojian
NSString *homePath = NSHomeDirectory();
NSLog(@"homePath=%@",homePath);
NSString *path = [homePath stringByAppendingPathComponent:@"文本.txt"];
//打开一个文件准备读取
//NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
//打开一个文件准备更新
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path];
NSData *data1 = [fileHandle readDataToEndOfFile];//读取数据
//将NSDataz转换成NSString类型
NSString *sdata1 = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
NSLog(@"content1=%@",sdata1);
//关闭
[fileHandle closeFile];
}
//在文件最后添加内容
void appendFileContent(){
//获取根路径 /Users/xiaoguojian
NSString *homePath = NSHomeDirectory();
NSLog(@"homePath=%@",homePath);
NSString *path = [homePath stringByAppendingPathComponent:@"文本.txt"];
//打开一个文件准备更新
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path];
NSString *append = @"这是追加的内容";
//将NSString类型的字符串转换成NSData类型的
NSData *appData = [append dataUsingEncoding:NSUTF8StringEncoding];
//写数据到文件中
[fileHandle writeData:appData];
//关闭
[fileHandle closeFile];
}
//在指定的位置添加内容
void appendFileContentInControl(){
//获取根路径 /Users/xiaoguojian
NSString *homePath = NSHomeDirectory();
NSLog(@"homePath=%@",homePath);
NSString *path = [homePath stringByAppendingPathComponent:@"文本.txt"];
//打开一个文件准备更新,只有更新的情况下才能对文件进行更新操作
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path];
//获取文件中的字节数,空格占一个,汉字占三个
NSUInteger length = [fileHandle availableData].length;
NSLog(@"length=%ld",length);
//在文件指定的位置添加内容,会替换掉节点后面的内容
[fileHandle seekToFileOffset:length/2];
NSString *append = @"这是追加的内容";
//将NSString类型的字符串转换成NSData类型的
NSData *appData = [append dataUsingEncoding:NSUTF8StringEncoding];
//写数据到文件中
[fileHandle writeData:appData];
[fileHandle closeFile];
}
//赋值一个文件中的内容到另外一个文件中
void copyFileContentToAnotherFile(){
//获取根目录
NSString *homePath = NSHomeDirectory();
NSString *resourcePath = [homePath stringByAppendingPathComponent:@"resource.txt"];
NSString *destPath = [homePath stringByAppendingPathComponent:@"dest.txt"];
//用NSFileManager创建文件
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager createFileAtPath:destPath contents:nil attributes:nil];
if (success) {
NSLog(@"create success!");
} else {
NSLog(@"create failure!");
}
NSFileHandle *resHandle = [NSFileHandle fileHandleForReadingAtPath:resourcePath];
NSFileHandle *destHandle = [NSFileHandle fileHandleForWritingAtPath:destPath];
//读取文件中的全部数据
NSData *content = [resHandle readDataToEndOfFile];
//NSData *content1 = [resHandle availableData];//这个也是读取文件中的全部数据
[destHandle writeData:content];
//关闭
[resHandle closeFile];
[destHandle closeFile];
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
//获取根目录
NSString *homePath = NSHomeDirectory();
NSString *resourcePath = [homePath stringByAppendingPathComponent:@"resource.txt"];
NSString *destPath = [homePath stringByAppendingPathComponent:@"dest.txt"];
//用NSFileManager创建文件
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager createFileAtPath:destPath contents:nil attributes:nil];
if (success) {
NSLog(@"create success!");
} else {
NSLog(@"create failure!");
}
NSFileHandle *resHandle = [NSFileHandle fileHandleForReadingAtPath:resourcePath];
NSFileHandle *destHandle = [NSFileHandle fileHandleForWritingAtPath:destPath];
NSUInteger l = 6;
//读取文件中的全部数据
NSData *content = [resHandle readDataOfLength:l];
//NSData *content1 = [resHandle availableData];//这个也是读取文件中的全部数据
[destHandle writeData:content];
//关闭
[resHandle closeFile];
[destHandle closeFile];
}
return 0;
}
oc中NSFileHandle的初学-文件读取
最新推荐文章于 2017-12-18 14:18:12 发布